I have on page div block:
<div class='btn'>click here</div>
<div class='dialogWindow'></div>
and js with click handler:
$('.btn').live('click', function(){
$.ajax({
type: "POST",
url: "services/service1.php"
}).done(function(result) {
$('.dialogWindow').empty().append(result).dialog();
// Handler for btn in HTML from service1
$('.newBtn').live('click', function(){
alert('click on new btn');
});
});
});
HTML from service1:
<div class='newBtn'>click here</div>
When user click on div.btn ajax return from service1 HTML with new div.newBtn. New HTML displayed in jQuery dialog-window. When user close dialog-window and open it again by clicking on div.btn and after that clicking on div.newBtn => click-event on div.newBtn run two times. How run click-event on div.newBtn only ones not dependency upon how many times user open jQuery dialog window?????
I think your problem is every time you click the btn you attach a new event handler to newBtn.
If you don't have any other event handlers on the button - your code can just remove existing click handlers before attaching new ones (sorta like this)
$('.btn').live('click', function(){
$.ajax({
type: "POST",
url: "services/service1.php"
}).done(function(result) {
$('.dialogWindow').empty().append(result).dialog();
// Handler for btn in HTML from service1
$('.newBtn').die( "click" );
$('.newBtn').live('click', function(){
alert('click on new btn');
});
});
});
In addition I'm not sure what version of jquery you are developing for but .live has been deprecated for quite a long time. If possible I would switch to on/off
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments