Check containing text

Description

The following code shows how to check if the element contains specific text.

Example


<html>
<head>
<script
  src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
  $(document).ready(function() {<!--from  ww  w. j av a 2  s  .  c  om-->
    $("div").one('click', function() {
      if ($(this).is(":first-child")) {
        $("p").text(":first-child");
      } else if ($(this).is(".blue,.red")) {
        $("p").text("It's a blue or red div.");
      } else if ($(this).is(":contains('java2s.com')")) {
        $("p").text("It's java2s.com!");
      } else {
        $("p").html("It's nothing <em>special</em>.");
      }
    });
  });
</script>
<style>
div.middle {
  color: red;
}
</style>
</head>
<body>
<body>
  <div>java2s.com</div>
  <div class="blue">jav a2s.com</div>
  <div class="red">java2s.com</div>
  <div>
    <span>java2s.com</span>java 2s.com
  </div>
  <div class="blue">java2s.co m</div>
  <p>java2s.com</p>
</body>
</html>

Click to view the demo





















Home »
  jQuery »
    jQuery Example »




Animation
DOM
Event
Form
List
Select
Style
Table
Utilities