jQuery Table How to - Change table cell text color based on text








Question

We would like to know how to change table cell text color based on text.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-1.6.4.js'></script>
<script type='text/javascript'>
$(window).load(function(){<!--from w  ww  . j a va2 s . c  om-->
    $(document).ready(function() {
       $('.status:contains("Paid")').css('color', 'red');
       $('.status:contains("Received")').css('color', 'green'); 
    });
});
</script>
</head>
<body>
  <div class="status">Paid</div>
  <div class="status">Received</div>
</body>
</html>

The code above is rendered as follows: