tags:

views:

35

answers:

2

How to add exception in this jquery code?

$(function() {
        $("table tr:nth-child(even)").addClass("striped");
      });

this code is applying on all tables.

but for specific pages i don't want strip effect.

I've different body id on each page.

I want to know how to add exception for a id.

$(function() {
        $("table tr:nth-child(even)").addClass("striped");
        //I want to add exception to not to add striped class to only to page with <body id="nostrip">
      });
+1  A: 
$('body[id!=nostrip] table tr:nth-child(even)').addClass("striped");

which can be reduced to

$('body[id!=nostrip] tr:even').addClass("striped");
David Hedlund
+4  A: 

David’s solution works if you only have to filter for one ID. However, since you have several body IDs for which you don’t want to use the script, you can use something like this:

$('body:not(#id1, #id2, #id3) tr:even').addClass('striped');
Mathias Bynens
+1 yes i need like this
metal-gear-solid
Can i add any css id and class? for example if same page has 2 table, and for one table i need strips and for one table i don't want.
metal-gear-solid
+1, i totally missed that part. @metal-gear-solid: if this solution solves your problem, please mark the tick below the vote-button to set it as the accepted answer.
David Hedlund
Please rename my question's title if it can be better.
metal-gear-solid
@metal-gear-solid: Sure, to only add stripes to `table`s with `class="stripes"`, use `$('body:not(#id1, #id2, #id3) table.stripes tr:even')` or shorter: `$('body:not(#id1, #id2, #id3) .stripes tr:even')`.
Mathias Bynens
@Mathias Bynens - Thanks Mathias it's great help for me. Hats off.
metal-gear-solid
@Mathias Bynens - Both option not working this is my `body id="email-alert"`
metal-gear-solid