JQuery - How to change the parent of an element from H1 to P?

Jquery - How to change the parent of an element from H1 to P?

I have <h1>heading</h1> , how can I change it to <p>heading</p>

I think that could be $.unwrap , then $.wrap , but is there a better way?

+4
source share
4 answers
 $('h1').wrapInner('<p/>').children().unwrap(); 
+8
source

This question is pretty close (I would consider a duplicate of it) How to change an element (e.g. h1 - gt; h2) using jQuery / plain old javascript?

Using this solution, your answer will be similar to

 var p = $('h4'); var a = $('<p/>'). append(p.contents()); p.replaceWith(a); 

Test it here: http://jsbin.com/abaja/edit

+1
source

I would create a new element with the contents of H1, then hide H1.

 var contents = $('h1').html(); $('h1').after('<p>'+content+'<p>'); $('h1').hide(); 
0
source

HTML:

 <h1 id="a">heading</h1> 

JQuery

 var $a = $('#a'); var contents = $a.contents(); $a.remove(); $('body').append('<p>' + contents + '</p>'); 

$.unwrap will remove the parent of the matching element. In other words, if #a was a child of a div , the div would be deleted, not h1 .

0
source

Source: https://habr.com/ru/post/1307828/


All Articles