March 21, 2012

jQuery: How can I wrap <a> and </a> tags around existing images in a div?

Question by lith0pedion

I’m using slidy.js to cycle through several images inside a div.

My question is: How can I use jQuery to wrap <a href="http://www.google.com"> and </a> around each image in the div (whose id is “imgFade”)?

Thanks in advance!

Adam

Answer by Starx

Yes you can use the .wrap()

Example:

$("#yourdiv img").wrap('<a href="http://www.google.com" />');
                      // Note: the self closing pattern ^ THIS is how you should define the wrapping element

This is wrap a link around all the images inside the div with id="yourdiv"

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!