Using jquery, How would I wrap a div class around the text inside all h1 tags. E.g to make all
<h1> test </h1>
in a document into
<h1> <div class="wrap">test </div></h1>
.
I am having a mental block. Any help would be much appreciated.
Try to use use $('h1').wrapInner('<div class="wrap" />');
But use a <span class="wrap" />
From the DOCS: http://api.jquery.com/wrapInner/
<div>
is not valid inside of <h1>
. You would be better off using a <span>
. Pure JavaScript:
var h = document.getElementsByTagName('h1')[0],
d = document.createElement('span');
while(h.firstChild) d.appendChild(h.firstChild);
h.appendChild(d);
d.className = "wrap";
But that said, can't you just apply the "wrap" class to the <h1>
?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With