Tuesday, April 3, 2012

jQuery wrap each char in except tags

This is how to wrap characters with html tag excluding the present html tags.

$("div").children().andSelf().contents().each(function(){

    if (this.nodeType == 3) {
        var $this = $(this);
        $this.replaceWith($this.text().replace(/(\w)/g, "<span>$&</span>"));

    }
});