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 divs on a page with the same class.
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 divs on a page with the same class.
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:
http://jsfiddle.net/rxAMF/
This may help you
First Word in String with jquery
Replacing HTML will result in event handlers being unbound and replacing the entire text for an element will result in HTML markup being lost. The best approach leaves any HTML untouched, manipulating only the first text node of the matching element. To get that text node, you can use
.contents()
and.filter()
:Working demo: http://jsfiddle.net/9AXvN/
Using this method will ensure that manipulating the first word will have no unwanted side effects on the rest of the element's content.
You can easily tack this on as an extension to jQuery, with an optional value for the number of words you want to wrap:
Working demo: http://jsfiddle.net/9AXvN/1/
This should work:
JSFiddle
basicly you can do like this