39

I've tried some HTML DOM code from several sites, but it isn't working. It isn't adding anything. Does anyone have a working example on this?

this.img = document.createElement("img");
this.img.src = "img/eqp/"+this.apparel+"/"+this.facing+"_idle.png";
src = getElementById("gamediv");
src.appendChild(this.img)

But it isn't adding anything to the div gamediv. I've tried document.body as well, with no result.

7 Answers 7

80

You need to use document.getElementById() in line 3.

If you try this right now in the console:

var img = document.createElement("img");
img.src = "http://www.google.com/intl/en_com/images/logo_plain.png";
var src = document.getElementById("header");
src.appendChild(img);
<div id="header"></div>

... you'd get this:

enter image description here

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

8 Comments

this.img = document.createElement("img"); this.img.src = "img/eqp/"+this.apparel+"/"+this.facing+"_idle.png"; src = document.getElementById("gamediv"); src.appendChild(this.img); Changed it now, but it still doesn't pop up in the div gamediv.
@klausbyskov: Thanks for fixing the typo.
@Anonymous: Could it be that the src path is incorrect? Can you try an image with an absolute path, that you know for sure exists? Such as: http://www.google.com/intl/en_com/images/logo_plain.png
Running the code it says src is null. But I am sure I made a div with gamediv as an id. <div id="gamediv"></div>
I know the src is correct, the comment above is what firebug tells me when I select break on error
|
7

With a little research i found that javascript does not know that a Document Object Exist unless the Object has Already loaded before the script code (As JavaScript reads down a page).

<head>
    <script type="text/javascript">
        function insert(){
            var src = document.getElementById("gamediv");
            var img = document.createElement("img");
            img.src = "img/eqp/"+this.apparel+"/"+this.facing+"_idle.png";
            src.appendChild(img);
        }
     </script>
 </head>
 <body>
     <div id="gamediv">
         <script type="text/javascript">
             insert();
         </script>
     </div>
 </body>

Comments

5

Use Image() instead

Instead of using document.createElement() use new Image()

const myImage = new Image(100, 200);
myImage.src = 'picture.jpg';
document.body.appendChild(myImage);

https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/Image

The Image() constructor creates a new HTMLImageElement instance. It is functionally equivalent to document.createElement('img').

Comments

4

This works:

var img = document.createElement('img');
img.src = 'img/eqp/' + this.apparel + '/' + this.facing + '_idle.png';
document.getElementById('gamediv').appendChild(img)

Or using jQuery:

$('<img/>')
.attr('src','img/eqp/' + this.apparel + '/' + this.facing + '_idle.png')
.appendTo('#gamediv');

Comments

2

or you can just

<script> 
document.write('<img src="/*picture_location_(you can just copy the picture and paste   it into the script)*\"')
document.getElementById('pic')
</script>
<div id="pic">
</div>

Comments

1

Get rid of the this statements too

var img = document.createElement("img");
img.src = "img/eqp/"+this.apparel+"/"+this.facing+"_idle.png";
src = document.getElementById("gamediv");
src.appendChild(this.img)

Comments

0

Things to ponder:

  1. Use jquery
  2. Which this is your code refering to
  3. Isnt getElementById usually document.getElementById?
  4. If the image is not found, are you sure your browser would tell you?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.