1

This code is linked to an html page

images = new Array();
images[0] = new Image();
images[0].src = "images/kate.jpg";
images[1] = new Image();
images[1].src = "images/mila.jpg";

document.write(images[0]);

is that the proper set up for the array and if it is how do i actually get the image to display on my page because when i run this it only returns "[object HTMLImageElement]"

Justin
  • 339
  • 1
  • 3
  • 15

3 Answers3

1

You could try this:

document.write(images[0].outerHTML);

Or this:

document.body.appendChild(images[0]);

But if you find yourself doing a lot of DOM manipulation, you should probably consider using a toolkit to make your life easier. In jQuery, for example you could simply write this as:

$("body").append($('<img src="images/kate.jpg" />'));
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
0

Try this:

document.write("<img src='"+ images[0].src +"'/>");
Ringo
  • 3,795
  • 3
  • 22
  • 37
0

You might want to take a look into JQuery. It seems like you are new to HTML and JavaScript so learning JQuery now will help you a lot. See this answer: jQuery add image inside of div tag

Community
  • 1
  • 1
benathon
  • 7,455
  • 2
  • 41
  • 70
  • Why would you need jQuery for this? It is something that could easily be done with just vanilla JavaScript. And since jQuery is JavaScript it is probably better to learn a little more about the language before you start getting into libraries. Yeah it can make things a lot simpler, but you don't need a whole library for something small like this. – Andrew Lively Dec 18 '13 at 05:00
  • The DOM is the worst thing that happened to JavaScript, as evidence here. On older browsers document.write() can do some crazy bad stuff to the dom. If jQuery is used properly, this new user may have an easier and more fun time learning JS. – benathon Dec 18 '13 at 08:40