84

I am trying to add an img to the placehere div using JavaScript, however I am having no luck. Can anyone give me a hand with my code?

<html>
<script type="text/javascript">
var elem = document.createElement("img");
elem.setAttribute("src", "images/hydrangeas.jpg");
elem.setAttribute("height", "768");
elem.setAttribute("width", "1024");
elem.setAttribute("alt", "Flower");
document.getElementById("placehere").appendChild("elem");
</script>
<body>

<div id="placehere">

</div>

</body>
</html>

5 Answers 5

86
document.getElementById("placehere").appendChild(elem);

not

document.getElementById("placehere").appendChild("elem");

and use the below to set the source

elem.src = 'images/hydrangeas.jpg';
Sign up to request clarification or add additional context in comments.

5 Comments

OK, I took the var out of quotes but I still don't see an image
@AndrewDeForest: appendChild expects an object and then it should work unless the path to your image is wrong. Check this jsfiddle.net/naveen/S57rs
Hmm, I have no idea. I changed it to elem.src = "images/hydrangeas.jpg" and I still don't see it. I even wrote out the tag <img src="images/hydrangeas.jpg"> just to make sure the path was correct, and it showed up.
Are you sure that the script runs?
I think my script was running before the DOM finished loading, so I added document.addEventListener("DOMContentLoaded", function() and it seemed to fix it!
34

It should be:

document.getElementById("placehere").appendChild(elem);

And place your div before your javascript, because if you don't, the javascript executes before the div exists. Or wait for it to load. So your code looks like this:

window.onload = function() {
  var elem = document.createElement("img");
  elem.setAttribute("src", "http://img.zohostatic.com/discussions/v1/images/defaultPhoto.png");
  elem.setAttribute("height", "768");
  elem.setAttribute("width", "1024");
  elem.setAttribute("alt", "Flower");
  document.getElementById("placehere").appendChild(elem);
}
<div id="placehere"></div>

To prove my point, see this with the onload and this without the onload. Fire up the console and you'll find an error stating that the div doesn't exist or cannot find appendChild method of null.

6 Comments

Forget all the setAttribute calls, just set the properties directly: elem.src = ...; elem.height = ...; elem.alt = ...; and so on.
Agreed, but as far as I know, using this method has no drawbacks. Maybe we should set up a performance test on JSPerf
JSPerf test shows that setAttribute is actually faster. Nice.
The difference in performance is negligible, but for the record, here's one that goes the other way in some browsers. More to the point is the inconsistency of setAttribute across browsers versus the consistency of setting properties. In HTML5 compliant browsers the two do the same thing, so it is hardly surprising that performance in such browsers is nearly identical.
Perhaps it's faster if you aren't appending it. That's what I deduce from that test. You're right about consistency, of course.
|
5
function image()
{
    //dynamically add an image and set its attribute
    var img=document.createElement("img");
    img.src="p1.jpg"
    img.id="picture"
    var foo = document.getElementById("fooBar");
    foo.appendChild(img);
}

<span id="fooBar">&nbsp;</span>

Comments

2

The following solution seems to be a much shorter version for that:

<div id="imageDiv"></div>

In Javascript:

document.getElementById('imageDiv').innerHTML = '<img width="100" height="100" src="images/hydrangeas.jpg">';

Comments

1

In case anyone is wondering how to do it with JQuery:

$("<img height='200' width='200' alt='testImage' src='https://avatars.githubusercontent.com/u/47340995?v=4'> </img>").appendTo("#container");

Ref: https://api.jquery.com/jQuery/#jQuery2

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.