1

If I have the following:

<ul>
  <li>Bob</li>
  <li>Chris</li>
  <li>Julie</li>
</ul>

Can I select just the li that has the `innerHtml' equal to "Chris"?

Something like

$('ul').child('li' html:'Chris'); // I dunno..
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188

1 Answers1

3

Use :contains() to select elements that contain the specified text.

$('ul li:contains(Chris)').css('color','red')
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
  <li>Bob</li>
  <li>Chris</li>
  <li>Julie</li>
</ul>

For exact match use filter() method

$('ul li').filter(function() {
  return $(this).html() == 'Chris';
}).css('color', 'red')
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
  <li>Bob</li>
  <li>Chris</li>
  <li>Julie</li>
</ul>
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188