1

I have a html5-document that contains this element:

<div id="imgContainer"></div>

The document loads, the user logs in, and some new text gets loaded and successfully displayed via Ajax (using jQuery). After a while (when the user makes a click) a new image should be inserted into the imgContainer-div.

I do this with this jQuery-command:

$("#imgContainer").html('<img src="newImage.png" alt="">');

The file newImage.png is located in the same directory as the html-file and the javaScript-file. But it does not appear in the browser-window. But it is correctly inserted into the source-code. I checked this with the developer-tools of my browser (safari). This tools don't report any error. But still the image is invisible. When I look into the list of resources I can see no newImage.png. Obviously the browser didn't load it. But why?

The image appears in the browser-window when I enter its URL. So the browser is able to load it. But it does not when I modify the html-document. Why?

Must I add some additional code to ma javaScript to tell the browser to load the image? If so: Can you tell me this code?

Edit:

try here: http://jsfiddle.net/YCs66/1/

11
  • You're code looks correct. Do you see the image file in the network tab of the developer tools (i.e. is the browser actually requesting the image)? Commented Jul 5, 2014 at 16:44
  • 1
    are you absolutely sure that your new <img> element is being injected into the page correctly? it is possible that your script executes before the container element loads, resulting the jQuery attempting to inject the image into an element that doesnt exist, is your code placed in a block that runs after DOM loads? here have a look, your code works fine in a fiddle: jsfiddle.net/TheBanana/LWpjr Commented Jul 5, 2014 at 16:53
  • 2
    what is <img scr= ?? scr means nothing, it should be src man. (referring to your jsfiddle) Commented Jul 5, 2014 at 17:56
  • 1
    @enhzflep - you don't need a closing slash for self-contained elements. it's not 2006 anymore. Commented Jul 6, 2014 at 10:44
  • 1
    @enhzflep: the 4th word in my posting is "html5" which means: Elements that have no content (like "img") don't need a slash. If you want you can write it, but a html5-document is also wellformed if you omit it. Commented Jul 8, 2014 at 6:48

2 Answers 2

4

http://jsfiddle.net/dwebexperts/ZCL2U/1/

Replace this

$("#imgContainer").html('<img scr="https://www.google.com/textinputassistant/tia.png" alt="">');

with this

 $("#imgContainer").html('<img src="https://www.google.com/textinputassistant/tia.png" alt="">');

As I checked in your Fiddle, you have given source attribute as "scr".

Sign up to request clarification or add additional context in comments.

Comments

0

The problem with your code is that the .html() method only puts in the HTML without actually parsing the attributes for the DOM element. Here is the solution

HTML

<form name="myForm" action="">
<input type="button" value="click me"
onclick="loadPic()">
</form>
<div id="imgContainer"></div>

Javascript

var loadPic = function () {
    alert("loadPic started");
    $("#imgContainer").html('<img/>');
    $("img").attr("src", "https://www.google.com/textinputassistant/tia.png");
    alert("loadPic finished");
};

PLAYGROUND


Note that the <img/> that is performing .attr("src", ...) is an asynchronous function. As a result, 2 alert()s will come out before the actual image is being loaded.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.