0

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">
      });

2 Answers 2

4

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');
Sign up to request clarification or add additional context in comments.

6 Comments

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.
+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.
Please rename my question's title if it can be better.
@metal-gear-solid: Sure, to only add stripes to tables 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 - Thanks Mathias it's great help for me. Hats off.
|
1
$('body[id!=nostrip] table tr:nth-child(even)').addClass("striped");

which can be reduced to

$('body[id!=nostrip] tr:even').addClass("striped");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.