Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the type of element using jQuery

If I know the ID of an element, is it possible to change the type of HTML tag that it has?

For example if you have <p id="p">Lots and lots of text here.</p>, is it possible to change it to <span id="p">....?

Thanks.

like image 421
David Gard Avatar asked Dec 09 '22 04:12

David Gard


1 Answers

You can use the replaceWith method to do this. You'll need to rebuild the attributes for the replacement though.

$('#p').replaceWith(function(){
   return '<span>' + $(this).contents().text() + '</span>';
});

Working example

like image 176
Jamie Dixon Avatar answered Dec 27 '22 00:12

Jamie Dixon