2

how to add link for the image created using following javascript.

thanks for any help or replies.

for(var i=0; i<images.length; i++) {
   var t = document.createElement('IMG');
   t.setAttribute('src',images[i]);
   t.setAttribute('border', 0);
   t.setAttribute('width',imageWidth);
   t.setAttribute('height',imageHeight);
   t.style.position = 'absolute';
   t.style.visibility = 'hidden';

   el.appendChild(t);
}
1
  • 2
    It's not a good idea to use setAttribute on HTML documents. There are many cases where this fails in IE (albeit not ones you hit here). Prefer the DOM Level 1 HTML properties: t.src= images[i]; etc. Commented Apr 12, 2010 at 11:04

3 Answers 3

5

Try this:

for(var i=0; i<images.length; i++) 
{

   var t = document.createElement('IMG');
   var link = document.createElement('a'); // create the link
   link.setAttribute('href', 'www.example.com'); // set link path
   // link.href = "www.example.com"; //can be done this way too

   t.setAttribute('src',images[i]);
   t.setAttribute('border', 0);
   t.setAttribute('width',imageWidth);
   t.setAttribute('height',imageHeight);
   t.style.position = 'absolute';
   t.style.visibility = 'hidden';

   link.appendChild(t); // append to link
   el.appendChild(link);
}
Sign up to request clarification or add additional context in comments.

Comments

0

Create an a element in the same fashion. Append it to el instead of the image and append the image to it.

Comments

0

You need to first create a anchor element then append the img element to it... like so:

for(var i=0; i<images.length; i++) {
   var a = document.createElement('a');
   a.href = "http://www.MYWEBSITE.com/"
   var t = document.createElement('IMG');
   t.setAttribute('src',images[i]);
   t.setAttribute('border', 0);
   t.setAttribute('width',imageWidth);
   t.setAttribute('height',imageHeight);
   t.style.position = 'absolute';
   t.style.visibility = 'hidden';
   a.appendChild(t);
   el.appendChild(a);
}

Then append the anchor to 'el'

Matt

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.