:contains() Selector in JQuery

 

Sample:

version added: 1.1.4jQuery(‘:contains(text)’)

The matching text can appear directly within the selected element, in any of that element’s descendants, or a combination thereof. As with attribute value selectors, text inside the parentheses of :contains() can be written as bare words or surrounded by quotation marks. The text must have matching case to be selected.

Example:

Finds all divs containing “John” and underlines them.

<!DOCTYPE html>
<html>
<head>
  <script src="/scripts/jquery-1.4.js"></script>
</head>
<body>
	<div>John Resig</div>

  <div>George Martin</div>
  <div>Malcom John Sinclair</div>
  <div>J. Ohn
<script>$("div:contains('John')").css("text-decoration", "underline");</script>
</body>
</html>

Leave a Reply

Your email address will not be published. Required fields are marked *