views:

1851

answers:

3

Advanced title, simple question:

How to do the following in jQuery:

$("table tr").click(function() {
    $("table tr:not(" + $(this) + ")").hide();
    // $(this) is only to illustrate my problem

    $("table tr").show();
});

Thanks in advance!

+7  A: 
$(this).siblings().hide();

Traversing/Siblings

Alexander Gyoshev
jQuery makes everything so much more elegant. +1!
JorenB
A: 

I think a solution can be this:

$("table.tr").click(function() {
    $("table.tr:not(" + $(this).attr("id") + "").hide(); // $(this) is only to illustrate my problem
    $(this).show();
})

--EDIT for Comment:

$("table.tr").click(function() {
    $("table.tr:not(#" + $(this).attr("id") + ")").hide(); // $(this) is only to illustrate my problem
    $(this).show();
})
andres descalzo
You mean `:not(#" + ...`. Also, this won't work unless the element has an ID, which is unlikely.
SLaks
Also, you mean ` + ")")...`
SLaks
this would require you to add random and unnecessary ids onto all your table rows (or whatever you're using).
nickf
@ SLaks, Thanks for the correction. On the dow, you may have in mind that sometimes what we seek is to have a Quick Answers to help. Why not take lightly what we put.
andres descalzo
@nickf, Yes, you're right, but that comment would be good first asking a @Kordonme if they have ID for each TR.
andres descalzo
+8  A: 
$("table.tr").not(this).hide();


As an aside, I think you mean $("table tr") (with a space instead of a dot).
The way you have it, it selects every table which has a class of tr (eg, <table class="tr">), which is probably not what you want.

For more information, see the documentation.

SLaks
Yeah, it was a mistake with the dot. I somehow fail to see how this is easier than Alexanders solution, which seems more clean. I know i asked how to do it with :not, but the siblings method just seems more clean.
Kordonme
glad you like it :) it's shorter, too!
Alexander Gyoshev