Skip to content Skip to sidebar Skip to footer

Use Jquery .filter() To Select Specific Text Within Div

HTML:

hello, my name is kevin. what's yours?

jQuery: $('p.greeting').filter(function (){ return $this.text() === 'my name is'; }).css('b

Solution 1:

Here you go:

CSS:

.highlight { background-color: yellow; }

JavaScript:

var text = 'My namE iS';

$( 'p.greeting' ).html( function ( i, html ) {
    var regexp, replacement;

    regexp = RegExp( '(' + text + ')', 'gi' );
    replacement = '<span class="highlight">$1</span>';

    return html.replace( regexp, replacement );
});

Live demo:http://jsfiddle.net/SGCDS/5/

Solution 2:

You're function returns always false. Only if there is exactly "my name is" in your div, it will return true.

Try something like this:

return $(this).text().indexOf("my name is") != -1;

Post a Comment for "Use Jquery .filter() To Select Specific Text Within Div"