tags:

views:

67

answers:

5
+1  Q: 

JavaScript images

How do I display the same image on the same page multiple times?

<head>
<Script language="javascript">
function xdf(){

for (i=0;i<10;i++) {

document.write('<b>hello world</b><br>');
}
}

</script>
</head>

this code displays "hello world" 10 times. i would like the same thing but with certain image instead of "hello word"...

+3  A: 

You can use document.createElement() to create an img element in JavaScript, and Node.appendChild() to append it to some other node:

var img = document.createElement('img');
img.setAttribute('src', 'my_image.png');
document.getElemenetById('some-div').appendChild(img);

The image will be loaded from the browser's cache if the above is repeated multiple times, appending each new img element to multiple nodes.


As @Matthew Flaschen suggested in a comment below, you could also use the Node.cloneNode() solution instead. You can create your img elment once:

var img = document.createElement('img');
img.setAttribute('src', 'my_image.png');

... and then use img.cloneNode(false) as an argument for appendChild():

document.getElemenetById('some-div').appendChild(img.cloneNode(false));
document.getElemenetById('some-other-div').appendChild(img.cloneNode(false));
Daniel Vassallo
No you can't . It will be moved. You would have to clone it.
Matthew Flaschen
@Matthew: You're right. Fixed my answer.
Daniel Vassallo
Daniel probably means "if you create a new image element, set src and append it to another element". That way there is no need to clone.
davyM
Rather than repeat all three lines, you could alternatively use [`cloneNode`](https://developer.mozilla.org/en/cloneNode).
Matthew Flaschen
@davyM: Yes, that's what is meant now. My previous answer was incorrect. Should be more clear now :)
Daniel Vassallo
+2  A: 
<img src="/path/to/img.png"/> <img src="/path/to/img.png"/>
Orbit
+2  A: 

you can clone the images:

<div><img src="http://www.google.com/favicon.ico" id="image" /></div>
<script type="text/javascript">
    n = 5;
    img = document.getElementById("image");
    for (i=0; i<n-1; i++) {
      img2 = img.cloneNode(false);
      img2.id = img.id + "_clone" + i;
      img.parentNode.appendChild(img2);
    }
</script>
mykhal
it does work!! Thanks!! But how to clone them only along left edge of window?? without right column??
DrStrangeLove
@DrStrangeLove you can e.g. add `style="float:left;clear:left"` to the `img`, and maybe some padding..
mykhal
+1  A: 

.. or maybe you'd rather want to define the background, using CSS?

<style type="text/css">
  body { background: url("http://www.google.com/favicon.ico") left repeat-y; }
</style>

(assuming from your previous comment, that you want to have the images in one column on the left edge)

mykhal
A: 

Change:

document.write('<b>hello world</b><br>');

Into:

document.write('<img src="IMAGE FILE NAME HERE.png" alt="TEXT HERE"><br>');
strager
thanks!! it works!!i tried to do it myself... but obviously i mistyped it.. :)
DrStrangeLove