tags:

views:

36

answers:

3

I wish to use JQuery to replace (backward and forward)

<a id="my_id">abc</a>

to

<span id="my_id">abc</span>

May I know how I can do so in JQuery?

Thanks.

+2  A: 
$("#my_id").each(function(){
     $(this).replaceWith("<a id=\"" + $(this).attr("id") + "\">" + $(this).text() + "</a>");
});
Сергій
actually he wants to change it to a span FROM a anchor but that's pretty close.
Erik
A: 
$('#my_id').replaceWith('<span id="my_id">abc</span>');
zdawg
A: 

This has the benefit of transfering all attributes from the anchor to the span (not just the id).

var cacheAttr = $('#my_id').attr();
var newSpan = $('<span></span>').attr(cacheAttr);
$('#my_id').replaceWith(newSpan);
Jon Erickson