Table cell conditional formatting based on content using jQuery
-
08-07-2019 - |
Question
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"); });
Solution
This is also covered in post jQuery select based on text.
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");
OTHER TIPS
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?
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow