views:

363

answers:

2

I simply have a table with a bunch of ✔ X and a few other symbols, how can I change the class of a cell based on it's contents?

jQuery example:

$(function(){$("td:has('✔')").addClass("tick"); });
$(function(){$("td:has('X')").addClass("cross"); });
A: 

This seems to work fine if the cell contains the symbol...

$("td:contains('✔')").addClass("tick");

...but if we are looking for something like hyphen (-) how ca nwe determine this is the only content of the cell instead of just containing it?

Any better solutions?

Peter
+2  A: 

This is also covered in post http://stackoverflow.com/questions/1430290/jquery-select-based-on-text/1430308#1430308.

You can widdle down the set of TD elements to only those td elements whose text exactly matches your expectations.

$("td")
  .filter
  (
    function()
    {
      return $(this).text() === "✔";
    }
  )
  .addClass("tick");
David Andres