What I have :
-
My text
- blahblahblah
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
var ul = document.getElementById("myId");
var li = ul.firstElementChild;
var text = li.firstChild;
var ul = li.childNodes[1];
ul.classList.add('myClass');
var span = document.createElement("span");
span.textContent = text.data;
li.replaceChild(span, text);
Old fashioned DOM to the rescue.