What I have :
<ul id="myId">
<li>
My text
<ul class="myClass">
<li>blahblahblah</li>
</ul>
</li>
</ul>
What I want :
<ul id="myId">
<li>
<span>My text</span>
<ul class="myClass">
<li>blahblahblah</li>
</ul>
</li>
</ul>
I dont have access to the HTML markup, and am wanting to do this with jQuery, Something like :
$('.myClass').get_the_text_ubove().wrap('<span>');
There must be some way of selecting 'My text' even though it has no class/id
Try:
$('#myId > li').each(
function(){
$(this.firstChild).wrap('<span></span>');
});
JS Fiddle demo.
With regards to wanting to add the class
to the ul
:
$('#myId > li').each(
function(){
$(this.firstChild).wrap('<span></span>');
$(this).find('ul').addClass('myClass');
});
JS Fiddle demo.
$($('#myId ul').addClass('myClass')[0].previousSibling).wrap('<span>');
JSFIDDLE DEMO
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