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 2 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.

Leave a Reply

  • Links

  • Calendar

  • September 2010
    S M T W T F S
    « Jun    
     1234
    567891011
    12131415161718
    19202122232425
    2627282930