Skip to main content

jQuery :Contains() Selector Examples

By Sam Deering

JavaScript

Share:

Free JavaScript Book!

Write powerful, clean and maintainable JavaScript.

RRP $11.95

It can be written as words or inside parentheses for example this contains function finds all the divs containing the word “jquery4u” and underlines it.

$("div:contains('jquery4u')").css("text-decoration", "underline");

Advanced example jQuery :Contains() Example 2

For this example this contains function finds all the paragraphs containing the word “jquery4u” and converts them to hyperlinks linking to the jquery4u blog.

var parag = $("p:contains('jquery4u')");
for(var i = 0; i < parag.length; i++){
for(var s = 0; s < kwords.length; s++){
var pattern = eval("/" + kwords[s]["kw"] + "/ig");
var originText = parag[i].innerHTML;
parag[i].innerHTML = originText.replace(pattern, "$&< /a>");

Sam Deering has 15+ years of programming and website development experience. He was a website consultant at Console, ABC News, Flight Centre, Sapient Nitro, and the QLD Government and runs a tech blog with over 1 million views per month. Currently, Sam is the Founder of Crypto News, Australia.

New books out now!

Learn valuable skills with a practical introduction to Python programming!


Give yourself more options and write higher quality CSS with CSS Optimization Basics.