Sohel Kapasi
Sohel Kapasi

Reputation: 1

My click on DIV is not getting triggered

<div class="SubMenu"><ul><li><a href="#">One</a></li><li><a href="#">Two</a></li><li><a href="#">Three</a></li></ul></div>

Now I wanted to fire a function when i click on div which has class "SubMenu" but the problem is i wanted to see if i have clicked there, i do not want to do like $(.'SubMenu').hide() instead i wanted to check that where i have clicked has a class SubMenu or not.

Issue here is i am not getting the element arrt dont know whats wrong, please help

Upvotes: 0

Views: 104

Answers (2)

rahul
rahul

Reputation: 187110

I am not sure what you are trying to achieve. I assume that you are looking to hide a menu on document click. If you are after this you can do something like this

$(function(){
    $(document).click(function(){
        if(!$(this).hasClass('submenu'))
        {
            $('div.submenu').hide();    
        }
    });
    $("div.submenu").click(function(e){
        e.stopPropagation(); // stops calling document click
    });
});

Upvotes: 0

tvanfosson
tvanfosson

Reputation: 532695

You can use hasClass to check if an element has a particular class.

$('div').click( function() {
      alert( $(this).hasClass('SubMenu') );
});

// outputs true or false

Upvotes: 4

Related Questions