Click html element other than dynamically generated elements?

I need to trigger a click on a html page other than div.but that the div is created dynamically after the page is loaded. I have tried the following code.

$('html').click(function(e) {   

    if(!$(e.target).hasClass('flyoutdiv') ){

    }

}); 

      

It only works for an element that is not dynamically generated. "flyoutdiv" is created dynamically after page load. I need to trigger a click on the whole page except for this element. Is there any solution?

+3


source to share


3 answers


You need to attach a click event to a flyoutdiv

div like below.

$(".flyoutdiv").on('click',function(e){e.stopPropagation();})

      



to handle dynamic flyoutdiv

you can use the following code.

$("body").on('click',".flyoutdiv",function(e){e.stopPropagation();})

+2


source


For an element created dynamically, you must use the on

.



$('html').on('click', function(e) {

  if (!$(e.target).hasClass('flyoutdiv')) {

  }

});
      

Run codeHide result


+2


source


After the flyoutdiv is created, bind an event listener to it. After div add is created$('.flyoutdiv').click(function(){//your code here});

This is much better than finding the target with each press.

+1


source







All Articles