jquery - First word selector -


How do I choose 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 would have to do this for multiple divs on the same class page.

Event handlers are unbounding as a result of HTML changes and as a result of changing whole text for an entry The best way to lose HTML markup is to leave any HTML insulated, only the first text of the mailing element connects to the node. To get that text node, you can use it and:

  overlay the function () {// only select node = $ ("div") for the node of the first node. Function () {return.nodeType == 3;}). First (), // Get the text ... text = node.text (), // ... and the first word first = text.slice (0, text index ("")); If (! Node. Lamps) return; // Remove the word before text node [0] .nodeValue = text.slice (first.length); // Add it back to the node with HTML. First ('& lt; span & gt;' + First + ' 



}; Content

You can easily do this in the form of jQuery extension, in which you want to wrap the number of words with an optional value:

  $ .fn WrapStart = function (nummed) {var node = this.contents (). Return to filter (function () {this.nodeType == 3}.) First (), text = node.text (), first = text.split ("", numword) .join (""); If (! Node. Lamps) return; Node [0] .nodeValue = text.slice (first.length); Node.before ('& lt; span & gt;' + + + '& lt; / span & gt; 



};

Working demo:

Comments