Wednesday, March 5, 2014

Jquery Change CSS upon table value

I was just checking out stackoverflow there was a chap on there that was trying to change the css of a table based on the value of the content. I remembered doing this on a company website so to archive it and to help others here is the simple solution I came up with to accommodate the requirements.

So the gist of this code is there is a style class that has been assigned in the header. There is a snippet of code in jquery that is where the magic happens. I select all the table elements by class you could also just select on the basic element td but in this case I went for a more practical approach since most likely you are not going to want to change css for every td on the site but who knows maybe you do. More power to you my friend. I use the each function to step through the different jquery objects in the array. For each object I pull the value and if it's negative I assign the class. It's really simple straight forward. I hope this helps someone and saves them some time.