jQuery Selector How to - Get an element, based on the text between the opening and closing tags








Question

We would like to know how to get an element, based on the text between the opening and closing tags.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-1.6.2.js'></script>
<script type='text/javascript'>
$(window).load(function(){<!--from  w w w.  j  a  v a2 s.c  o m-->
    $('div:contains("test")').css('background-color', 'red');
});
</script>
</head>
<body>
  <div>This is a test</div>
  <div>Another Div</div>
</body>
</html>

The code above is rendered as follows: