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?
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();})
For an element created dynamically, you must use the on
.
$('html').on('click', function(e) {
if (!$(e.target).hasClass('flyoutdiv')) {
}
});
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.