Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fire ul tag click event not li with jquery

How can affect click event only ul tag not all li with jQuery?

<!-- HTML -->
<ul class="wrap">
    <li>test1</li>
    <li>test2</li>
    <li>test3</li>
</ul>

I tried jquery like this.But it doesnt work.

//Javascript
jQuery("ul.wrap").not(jQuery("ul > li")).click(function(){
      //fire only ul
});

How can we do this?

like image 312
user3763026 Avatar asked Feb 12 '23 16:02

user3763026


1 Answers

You can simply do it with this code:

jQuery('.wrap').click(function (event) {    
    if ( !$(event.target).is( "li" ) ) {
        console.log('ul clicked!');
    } 
});

You can see an example with background colors that shows the ul and the li here: http://jsfiddle.net/S67Uu/2/

like image 164
John Skoumbourdis Avatar answered Feb 15 '23 04:02

John Skoumbourdis