7

say I have some table rows:

<tr class="toplevel" data-id="3">
 ...
</tr>
<tr data-id="3">
 ...
</tr>
<tr data-id="3">
 ...
</tr>

So as far as I know I can hide the ones with class toplevel like:

$('tr.toplevel').hide();

and I can hide the ones with data-id=3 like:

$('tr').data('3').hide();

However what I really want to do is hide the ones with data-id=3 that DON'T have the class toplevel.

Could someone please explain to me how to do this?

user2005657
  • 607
  • 10
  • 20

3 Answers3

9

You can use attribute selector within [] notation and use [:not] to exclude the ones with class .toplevel

 $('tr:not(.toplevel)[data-id="3"]').hide(); 
    ^   ^                      ^
    |   |                      |
all trs but .toplevel of which select the ones with data-id attribute value 3

Or

$('tr:not([class="toplevel"])[data-id="3"]').hide(); //Less efficient though due to explicit attribute name class

See Attribute Selectors

:not Selector

Fiddle

Community
  • 1
  • 1
PSL
  • 123,204
  • 21
  • 253
  • 243
3

Try this

$('tr:not(.toplevel)[data-id="3"]').hide();

or

$('tr[data-id="3"]').not('.toplevel').hide();

hide is a method that works for jQuery objects

$('tr').data('3') returns a string . So it will throw an error when you try to apply the hide method on it.

Sushanth --
  • 55,259
  • 9
  • 66
  • 105
-2

Try This....

$('tr[class!="toplevel"]').data('3').hide();

The Selector will get u all the tr(s) that dont have toplevel class.Then you can perform any actions on that elements

---Edited---- Try This $('tr[class!="toplevel"][data-id="3"]').hide();

  • Please refer this post: http://stackoverflow.com/questions/5309926/how-to-get-the-data-id-attribute-using-jquery May be some JQuery Conflicts for not working... – Sai Prasad Sabeson Jun 21 '13 at 02:56