Enes Kristo
Enes Kristo

Reputation: 31

How to pick all elements but the one i hover in jquery

I want to make all divs in a .html file disappear while the mouse is hovering on a certain div. In other words, is there anything like the $(this), but instead of this for all other divs that?
Thank you in advance.

Upvotes: 1

Views: 67

Answers (2)

Try

var div_all = $('div'); //refers to all div
div_all.hover(function () {
    div_all.not(this).hide(); //hide all div's but not current one
    $(this).show(); //$(this) refers to current div and show current div
}, function () {
    div_all.hide(); //hide all divs
});


div_all.not(this) refers to all divs except the one which is hovered.
References

this keyword

.not()

.hover()

Upvotes: 2

Bhadra
Bhadra

Reputation: 2104

$('div').hover(
    function(){ $('div').not(this).hide(); },
    function(){ $('div').show(); }
);

Upvotes: 2

Related Questions