Nighina t t
Nighina t t

Reputation: 140

click on html element except a dynamically created elements?

I need to trigger click in an html page except a div.but that div is created dynamically after page load.I have tried following code.

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

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

    }

}); 

It is working only for element that is not created dynamically. "flyoutdiv" is created dynamically after page load.I need to trigger click in the entire page except that element.is there any solution?

Upvotes: 3

Views: 69

Answers (3)

MysteriousLab
MysteriousLab

Reputation: 394

After you created flyoutdiv, bind a event listener to him. After creation of div add $('.flyoutdiv').click(function(){//your code here});

It is much better than searching for target on every click.

Upvotes: 1

Prashant Shirke
Prashant Shirke

Reputation: 1423

You need to attach click event to flyoutdiv div like following.

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

to handle dynamic flyoutdiv you can use the code like following.

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

Upvotes: 2

R3tep
R3tep

Reputation: 12864

You need to use the method on for element created dynamically.

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

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

  }

});

Upvotes: 2

Related Questions