tags:

views:

114

answers:

4

I am a novice jQuery student and need a bit of help. Is it possible for Jquery to change a <div> to an <a> dynamically depending on what the class of the <div> is? I have searched and can't find any reference to doing something like this using .add(), .append() or .html()

For example I want to change this:

"<div class="item"><div class="caption">pic1</div></div>"

to this:

"<a class="item" href="#"><div class="caption">pic1</div></a>"

Thanks for any help you may give or advice. Mike

+6  A: 

You can try jQuery.replaceWith(). You will probably want to use that in conjunction with jQuery.get().

Another option would be to use the manipulation functions to get the children of your tag, unwrap the tag, and then wrap the children with the desired tag.

jQuery('.item').children().unwrap().wrap('<a></a>');

However, as Andy pointed out in the comments, a div cannot exist inside of an a tag. That is not semantically correct HTML. What benefit are you trying to derive from doing this?


Edit: I saw you changed your question as I was updating my answer. Seems like you'd be better off with doing something like this instead (Different version mentioned by Silkster):

jQuery(document).delegate('.item', 'click', function (e) { /* Do something here */ });

jQuery.delegate()

Jeff Rupert
I am trying to integrate a jQuery lightbox, Yoxview into a coverflow like image gallery called ContentFlow http://www.jacksasylum.eu/ContentFlow/. I need only the "active" center image to open in a lightbox when clicked, not every image. Clicking on any other image moves it to the center active position. It seems if I use "<a href>" for the images, every clicked image opens in the lightbox, not just the center one, so I was trying to think of a way for jQuery to alter the active image into a link. With other lightboxes, I have used ".addclass" to the "<div>" and it will open but not yoxview.
Mike
@Mike: Check to see if Yoxview has an `open` method or something to that effect. You could then use that to dynamically decide what you'll actually be opening. I use Shadowbox (http://www.shadowbox-js.com/), personally, and it has an open function that works very nicely.
Jeff Rupert
Thanks Jeff, I have successfully used Shadowbox in this configuration in the past. I will check Yoxview's API.
Mike
A: 

http://api.jquery.com/replaceWith/

.append and .html can do it

Enrico Pallazzo
A: 

I would suggest you check out jQuery wrap and unwrap They should be able to do what you like.

David Mårtensson
+1  A: 

So, yeah, it is possible ... you should be able to use a selector by class like:

$('div.item')

... to the update using the replaceWith() like:

$('div.item').replaceWith('<a class='item'></a>');

... and then for the content you can just get the content and updated later using the each function like:

$('div.item').each(function (){
     var divContent = $(this).html();
     $(this).replaceWith('<a class='item'></a>').html(divContent);
});
Darkxes