Hide all but $(this) via :not in jQuery selector

JqueryJquery SelectorsThis

Jquery Problem Overview


Advanced title, simple question:

How can I do the following in jQuery (hiding everything except $(this))?

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

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

Jquery Solutions


Solution 1 - Jquery

$(this).siblings().hide();

Traversing/Siblings

Solution 2 - Jquery

$("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.

Solution 3 - Jquery

If you want to combine not() with some other selectors, you can use add():

$('a').click(function(e){
  $('a').not(this).add('#someID, .someClass, #someOtherID').animate({'opacity':0}, 800);
});

This would fadeout all other links, but the clicked one, and additionally fadeout some chosen ids and classes.

Solution 4 - Jquery

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKordonmeView Question on Stackoverflow
Solution 1 - JqueryAlex GyoshevView Answer on Stackoverflow
Solution 2 - JquerySLaksView Answer on Stackoverflow
Solution 3 - JquerylenoohView Answer on Stackoverflow
Solution 4 - Jqueryandres descalzoView Answer on Stackoverflow