lith0pedion
lith0pedion

Reputation: 27

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

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

Upvotes: 0

Views: 2140

Answers (4)

Ryan Buddicom
Ryan Buddicom

Reputation: 1151

jQuery docs (http://api.jquery.com/wrap/) seem to recommend including the closing tag, which all the other answers have self-closed. ie:

$("#imgFade img").wrap("<a href='http://www.google.com'></a>");

Upvotes: 0

Starx
Starx

Reputation: 79021

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"

Upvotes: 3

kinakuta
kinakuta

Reputation: 9037

You can use jQuery's .wrap() method:

$('div#imgFade img').wrap('<a href="http://www.google.com" />');

Upvotes: 1

benedict_w
benedict_w

Reputation: 3608

$('#imgFade img').wrap('<a href="http://www.google.com" />');

Upvotes: 8

Related Questions