santa
santa

Reputation: 12512

Wrap <LI> text with jQuery

I need to wrap the text within unordered list with <span>

Trying this, but it does not seem to work...

$('li').each(function(){
    var text = $(this).text();
    text = text.replace(/^(\w){1}/, "<span>$1</span>");
    $(this).html(text);
});

Upvotes: 1

Views: 1611

Answers (3)

Ram
Ram

Reputation: 144689

You can use wrapInner method:

$('li').wrapInner('<span/>')​​​​​​;

http://jsfiddle.net/UrTw5/

Upvotes: 2

Curtis
Curtis

Reputation: 103368

$('li').each(function(){
    var text = $(this).text();
    text = "<span>" + text + "</span>";
    $(this).html(text);
});

Upvotes: 1

wirey00
wirey00

Reputation: 33661

You can just use jQuery's .wrap() method

$('li').each(function(){
    $(this).contents().wrap('<span></span>');       
});

http://jsfiddle.net/7HdER/

Upvotes: 3

Related Questions