"Uploading" a picture using jQuery

I have been using this particular code snippet to display messages.

setTimeout(function(){ 
    o.html(o.html() + "Username:<br>" + msg[r] + "<br><hr>") 
}, 7000);

It's effective in posting messages from an array and adding some styling.

However, the current output of a posted message looks like this:

Username:
messagehere

<hr>

What I aim for is to have the code display an image before the username and message, similar to what you would see on Facebook when engaging with someone through chat or comments.

I attempted to simply add +<img src="" class=""/> in front of it, but that caused the code to stop functioning altogether.

Therefore, I am seeking guidance on the correct approach to achieve this. I would like to assign a class to it so that I can style the entire block with CSS (having the avatar preceding the message and username, as mentioned earlier).

If anyone could assist me in finding the proper solution, I would greatly appreciate it.

Answer №1

Make sure to always wrap the img tag in quotes to avoid any errors. If not enclosed in quotes, browsers might mistake it for a variable or function instead of a string literal.

+ '<img src="" class=""/>'

Additionally, consider using the .append() function rather than continuously adding to the .html(). This can help improve performance and prevent issues.

o.append("Username:<br>" + msg[r] + "<br><hr>" + "<img src='' class='' />"); 

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Reduce the dimensions of a CSS attribute automatically

Displayed below is the image with a width of 192 and height of 109: <a href="https://www.blogger.com/u/1/blogger.g?blogID=4402911157743442948#"><img alt="" class="thumbnail" height="109" src="https://2.bp.blogspot.com/-E5ftfbCCgkw/XjnLpO347cI/AAA ...

Weird Javascript behavior when classes are manipulated during mouse scrolling

Searching for a Javascript expert to assist in solving a particular issue. I have a simple function that I need help with. The goal is to add a bounceDown class when scrolling down by 1px, have it run for 5 seconds, and then remove the class for future use ...

How can I repeatedly substitute a word in the ajax response object?

Let's say we have an AJAX response object named 'var data;' that includes HTML content. Inside the content, there is a table with the id of 'table123'. The task at hand is to replace all instances of the word 'sample' wit ...

Tips on choosing just the selected checkbox values

In my CodeIgniter view, I am utilizing AJAX to post data to my controller. <script type="text/javascript"> $(document).ready(function(){ // find the input fields and apply the time select to them. $('#sample1 inp ...

How to Configure UpdatePanel Triggers for __doPostBack Events?

I have a situation where I need to set up triggers for an UpdatePanel on my webpage: <asp:updatepanel id="updatepanel1" runat="server"> <contenttemplate> <asp:label id="lblfoo" runat="server /> </contenttemplate> ...

React-Tooltip trimming

Currently, I am facing a dilemma that requires some resolution. The issue at hand is related to the placement of React-Tooltip within the List element. Whenever it's positioned there, it gets clipped. On the other hand, placing it at the bottom isn&a ...

Is it possible to create a return using messageEmbed in Discord?

I have been working on creating a Discord bot that responds to the ! status command with the server status. I took inspiration from this existing bot. The only change I made was to ensure that the bot replies immediately to the ! status command without re ...

Displaying mysqli results within a div while incorporating an onclick event for a javascript function that also includes another onclick event for a separate javascript function

I'm struggling to figure out the correct way to write this script. Can someone please guide me in the right direction or suggest an alternative method? I've searched but haven't found any relevant examples. I am fetching information from a ...

How to Print a Web Page in ASP.NET Without Header and Footer

Is there a way to Print a Web Page on Asp.Net without including the Header and Footer? ...

Update the form action URL when a specific option is selected from the dropdown menu upon clicking the submit

I would like my form to redirect to a specific page on my website based on the selection made in the <select> tag. For instance, if '2checkout' is selected, it should go to gateways/2checkout/2checkout.php. Similarly, if 'Payza' i ...

Upgrade minimist is available

Recently, an error message has been appearing on my GitHub account: Upgrade minimist to version 0.2.1 High severity Vulnerable versions: < 0.2.1 Patched version: 0.2.1 Minimist before 1.2.2 could be manipulated into adding or changing properties of O ...

Attempting to hide anchor tags for "register" and "login" while making the anchor tag for "logout" visible after signing in

After signing in, I want the register and login anchor tags to disappear and the logout anchor tag to appear. However, the login and register links stay visible on the page even after I sign in. <div class="navbar-nav ms-auto"> <% ...

Showing a JavaScript variable on an HTML page

I have a challenge where I am attempting to showcase a variable using the code provided below: HTML: <div id="MyEdit">Money</div> JS: var price1 = 0; var current_value=document.getElementById("MyEdit").innerHTML; if (current_value == "msc" ...

How can you show a green check mark next to an input field in AngularJS after inputting valid data?

I am diving into the world of AngularJS and Angular Material with my web application. As a beginner in AngularJS and Angular Material, I need some help. My current task is to display a green checkmark next to an input field only when valid data is entere ...

What is the best way to align bars at the bottom of a CSS vertical bar chart?

Currently, I am exploring the functionality of two distinct bar charts that I designed using HTML and CSS: one horizontal and the other vertical. Is there a way to shift the bars in my vertical bar chart from the center down to the bottom? https://i.sstat ...

A warning has been issued: CommonsChunkPlugin will now only accept one argument

I am currently working on building my Angular application using webpack. To help me with this process, I found a useful link here. In order to configure webpack, I created a webpack.config.js file at the package.json level and added the line "bundle": "web ...

Adjust the size of a div element after updating its content using Mootools

I am currently utilizing mootools 1.2 on a basic webpage. There is a div that contains a form (referred to as formdiv) which is submitted through JS/ajax. Once the ajax request returns a "confirmation message" data, the formdiv updates accordingly. The is ...

The POST function in jQuery often returns the [Object object] result

Looking to extract a string from a PHP file using jQuery's post method. function getString(string) { return $.ajax({ type: 'POST', url: 'scripts/getstring.php', data: { 'string': string } ...

Manipulating the DOM by nesting an icon within a button element in HTML

Currently working on my todo list project and I have a question about using innerHTML. I'm curious if I can include an icon inside of a button tag like this: btn.innerHTML = '<i class="fas fa-times fa-lg"></i>'; So, wo ...

Is it possible to implement the same technique across various child controllers in AngularJS?

I am trying to execute a function in a specific child controller. The function has the same name across all child controllers. My question is how can I call this function from a particular controller? Parent Controller: app.controller("parentctrl",functi ...