Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

jquery

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

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

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

like image 752
Kyle Avatar asked Jan 22 '23 05:01

Kyle


2 Answers

$('h1').wrapInner('<p/>').children().unwrap();
like image 66
Amry Avatar answered Jan 24 '23 17:01

Amry


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

Using this solution, your answer would resemble

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

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

like image 43
Rabbott Avatar answered Jan 24 '23 18:01

Rabbott