:contains()S2C Home « Selectors « :contains()

Containing text selector.

Shorthand version $(':contains(text)')

Description

The :contains() selector, selects all elements containing the specified text.

The text can appear in the specified element and/or descendants thereof.

If this selector is not preceded by another selector, the universal selector ("*") is implied and so the whole DOM will be searched. Use another selector as in the example below to narrow the search and improve performance.

Syntax


Signature Description
jQuery(':contains(text)')Text match

Parameters

Parameter Description
textThe text to search for.

Return

N/A.

:contains() ExampleTop

Selects all elements containing the specified text.

The following example will check for the text 'the' in 'em' elements and apply an orange background when the button below is clicked.


$(function(){
  $('#btn17').on('click', function() {
    $("em:contains('the')").css('backgroundColor', 'orange');
  });
}); 

Press the button below to action the above code: