jQuery :contains() Selector

jQueryWeb DevelopmentFront End Technology

The :contain() selector in jQuery is used to select elements containing the specified text.

Syntax

The syntax is as follows −

$(":contains(text)")

Here, the parameter text is the text to find. The same elements are selected with the specified text.

Example

Let us now see an example to implement the :contains() selector −

<!DOCTYPE html>
<html>
<head>
<style>
   .one {
      color: brown;
      background-color: orange;
      font-size: 16px;
      border: 2px blue dashed;
   }
</style>
<script src="https://code.jquery.com/jquery-3.4.1.js"></script>
<script>
   $(document).ready(function(){
      $("p:contains(the)").addClass("one");
   });
</script>
</head>
<body>
<h1>Examination</h1>
<p>Timings: 10AM - 1PM</p>
<p>The examination hall details would be emailed to the students.</p>
</body>
</html>

Output

This will produce the following output −

raja
Published on 05-Nov-2019 11:31:51
Advertisements