On site load, how do you wrap all images in a link dynamically using Javascript?

Posted by akaii on Stack Overflow See other posts from Stack Overflow or by akaii
Published on 2010-03-23T08:41:47Z Indexed on 2010/03/23 8:53 UTC
Read the original article Hit count: 298

Filed under:
|
|
|

I have barely any experience working with DOM and using Javascript, and I have a very specific task that I'm trying to accomplish.

Let's say I have an image in my HTML:

<img src="foo.jpg" />

When the site loads, I want to take that image (all images in the document, actually), and wrap them in a link:

<a href="http://www.foobar.com"><img src="foo.jpg" /></a>

What could I use to accomplish this? Google hasn't turned up much for me with regards to this specific task. On load, I can access and iterate all the images in the document... but I'm not sure where to go from there in order to wrap the image in a link.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about dom