5th June 2010

jQuery Conditional Formatting

posted in jQuery |

The jQuery contains and not functions are great for doing conditional formatting.  Along with the power of chaining in jQuery you can easily change the look of something in the browser window through a simple jQuery call.

For example, suppose you have a grid view and you are grouping by a certain column and you need each group to total 100% in one of its columns.  (Note: I am not going into detail of which grid view control you use.)  You could do these 2 lines of code:

  1. $(”tr[type=TotalRow]:contains(’100.00%’)”).addClass(”Valid”);
  2. $(”tr[type=TotalRow]:not(:contains(’100.00%’))”).addClass(”Invalid”);
  • $(”tr[type=TotalRow] - This will select all table rows that have an attribute named type which has a value of TotalRow.
  • :contains(’100.00%’)”) - This will further filter the list of table rows returned by ensuring that they have the text ‘100.00%’ in them.
  • .addClass(”Valid”); - This adds a CSS class of name ‘Valid’ to every table row that met the criteria.  In this case, every table row that has an attribute named type and a value of TotalRow as well as having the text 100.00% will have the ‘Valid’ CSS class applied to it.

The second line of code simply uses the not function so that it finds any table row that IS of type TotalRow and that does NOT have 100.00% in the row somewhere and then adds a CSS class named Invalid.

Pretty simple and easy to do.

This entry was posted on Saturday, June 5th, 2010 at 1:32 am and is filed under jQuery. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

There are currently 9 responses to “jQuery Conditional Formatting”

Why not let us know what you think by adding your own comment! Your opinion is as valid as anyone elses, so come on... let us know what you think.

  1. 1 On July 30th, 2010, forex robot said:

    nice post. thanks.

  2. 2 On August 10th, 2010, WP Themes said:

    helped me a lot in my college assignment. Thank you for your information.

  3. 3 On September 16th, 2010, Issin said:

    I just linked this article on my facebook account. it’s an interesting read for everyone.

  4. 4 On November 4th, 2010, as said:

    tyvm for sharing this..

  5. 5 On November 4th, 2010, oi said:

    Thanks for writing this, it was quite helpful and helped quite a bit

  6. 6 On November 6th, 2010, Wms Sydney said:

    Tyvm for the useful information! I would never have gotten this myself!

  7. 7 On December 21st, 2010, nex review said:

    thanks !! very helpful post!

  8. 8 On February 4th, 2011, posic said:

    This post actually made my day. You can not imagine just how much time I had spent for this information! Thanks!

  9. 9 On July 9th, 2011, jennifer said:

    This post helped me in my college assignment. Thanks Alot

Leave a Reply

  • Links

  • Calendar

  • February 2012
    S M T W T F S
    « Jan    
     1234
    567891011
    12131415161718
    19202122232425
    26272829