How can I select the first word in a div?
I need to be able to insert a line break after the first word, or wrap it in a span tag. I need to do this for multiple di
Actually, if you want to use it as a plugin, so that it would do it for all the items in the selector, not only the first one, use this one:
$.fn.wrapStart = function(numWords){
return this.each(function(){
$this = $(this);
var node = $this.contents().filter(function(){
return this.nodeType == 3
}).first(),
text = node.text(),
first = text.split(" ", numWords).join(" ");
if (!node.length) return;
node[0].nodeValue = text.slice(first.length);
node.before('' + first + '');
});
};
http://jsfiddle.net/rxAMF/