tags:

views:

66

answers:

4

like we select p:first-letter? I know there is no property called p:first-word but if any other way SO user knows.

I don't want to add anything in HTML.

+1  A: 

This isn't difficult in JavaScript - there's example code here (you'll need to view source to see how it works, but it does work). It doesn't depend on jQuery or any other libraries, as far as I can see.

Dominic Rodger
this is using <span> which is not my goal
metal-gear-solid
@Jitendra - not sure what you mean - it doesn't require adding anything to the original markup - it adds the span for you, which you could then control using normal jQuery selectors. How does that not answer your question?
Dominic Rodger
A: 

You can do it in JavaScript by finding the DOM node you are interested on and taking the .innerHTML of it, adding around the first word and storing back to .innerHTML.

Tronic
+3  A: 
$('p').each(function(){
    var me = $(this)
       , t = me.text().split(' ');
    me.html( '<strong>'+t.shift()+'</strong> '+t.join(' ') );
  });

This bolds first word.

or

$('p').each(function(){
    var me = $(this);
    me.html( me.text().replace(/(^\w+)/,'<strong>$1</strong>') );
  });
c0mrade
A: 

You can extract the full text from the p and grab the first word with a regexp:

$('p').click(function() {
    var word = /^\w+/.exec($(this).text());
    alert(word);
});
kemp