1

How do I find certain text on my web page and make it underlined?

For example, I have some text on my page and I want to find all the words hello and make them underlined.

My trial code is:

$( "body:contains('hello')" ).css( "text-decoration", "underline" );

But this ends up underlining the whole line instead of just that word.

oshirowanen
  • 15,297
  • 82
  • 198
  • 350

3 Answers3

3

Try this.

HTML

<p>For example, I have some text on my page and I want to find all the words hello and make them bold.</p>

Script

$('p').html( $('p').html().replace(/hello/g, '<strong>hello</strong>') )

Fiddle Demo

Rakesh Kumar
  • 2,705
  • 1
  • 19
  • 33
1
 $('body').html($('body').html().replace(/hello/g, '<strong>hello</strong>') )
monu
  • 370
  • 1
  • 10
  • this won't work! he want to make it bold not to add border to whole sentence. –  Mar 06 '14 at 09:33
  • This kind of works because I replaced the css with "`font-weight","bold"`, however, instead of making that single word bold, it makes the whole line bold. – oshirowanen Mar 06 '14 at 09:34
0
$(function() {
     $('*:contains("hello")').css( "text-decoration", "underline");
});

please review this code

Karthick Kumar
  • 2,349
  • 1
  • 17
  • 30