jQuery Selector :contains()

Introduction

The :contains() selector selects elements containing the specified string.

The string can be contained directly in the element as text or in its child element.

The text is case sensitive.

$(":contains(text)")
Parameter OptionalDescription
text Required. Specifies the text to find

Select all <p> elements containing "is":

View in separate window

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $("p:contains(is)").css("background-color", "yellow");
});//from  w  w  w  .  ja  v a 2s  .  c o  m
</script>
</head>
<body>

<h1>Welcome to My Homepage</h1>

<p class="intro">This is a test.</p>
<p>I am from java2s.com.</p>
<p>This is another test.</p>

Who is your favourite:
<ul id="choose">
  <li>Javascript</li>
  <li>HTML</li>
  <li>CSS</li>
</ul>

</body>
</html>



PreviousNext

Related