javascript - Bind click handler to all elements except those of a certain HTML tag -
so have page setup lets expend text boxes upon click:
it adds 'active' class elements upon click , removes if click text box. i'd remove class if click on red background. i'll want add elements page shortly it's important removal comes clicking red portion <body>
element.
so here's have on page now: (note log replaced removal of class)
$('*:not(li)').click(function(){ console.log('foo') });
the problem logs 'foo' everywhere click, directly on text boxes li
elements. how can make work upon click of background?
thanks!
your problem you're bubbling down html stack. putting handler on everything, if click on li, event bubble ul. similarly, $('body') cause bubbling issues.
you're best off creating div outside of main stack handle this. imagine absolutely positioned div behind of page.
if want have everything fire except li, code need more this:
$('*:not(li)').click(function(){ // something. }); $('li').click(function(evt){ // else. evt.stoppropagation(); });
Comments
Post a Comment