Cheok Yan Cheng
Cheok Yan Cheng

Reputation: 42670

Use JQuery to Replace type of tag with same ID

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.

Upvotes: 1

Views: 1616

Answers (3)

Zoli
Zoli

Reputation: 1137

Use this method:

$('#my_id').replaceWith('<span id="my_id">abc</span>');

Upvotes: 1

Jon Erickson
Jon Erickson

Reputation: 114846

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);

Upvotes: 0

Serhiy
Serhiy

Reputation: 4507

$("#my_id").each(function(){
     $(this).replaceWith("<a id=\"" + $(this).attr("id") + "\">" + $(this).text() + "</a>");
});

Upvotes: 3

Related Questions