Question

Here's what I have:

        $(document).click(function(event) {
            if ($(event.target) == $("a.nav-search")) {
                console.log('search click');
            }else{
                console.log('no search click')
            }
        });

I'm trying to make it so if you click on a.nav-search do one thing, but if you click anywhere else, do another thing. When I try this, I just keep logging "no search click". I've console logged my event.target and I get this:

[a.nav-search, context: a.nav-search, constructor: function, init: function, selector: "", jquery: "1.7.1"…]

What am I doing wrong?

Was it helpful?

Solution

Answer

$(document).click(function(event) {
    if ( $(event.target).is('a.nav-search') ) {
        console.log('search click');
    } else {
        console.log('no search click')
    }
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top