Skip to content Skip to sidebar Skip to footer

Wrap Each Char In Except Tags With Jquery

I'm trying to wrap each text character inside a
with a span tag. No problem there, it's just that I also need to keep nested tags unmodified. So

Solution 1:

You can do this by using the following code:

$("div").children().andSelf().contents().each(function(){
    if (this.nodeType == 3) {
        var $this = $(this);
        $this.replaceWith($this.text().replace(/(\w)/g, "<span>$&</span>"));
    }
});

See test case on jsFiddle.

Post a Comment for "Wrap Each Char In Except Tags With Jquery"