How to stop multiple event handler onclick on html from servise resnonse?

Darien Fawkes

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?????

noveyak

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to stop execution of click event from onchange event handler

From Dev

How to stop event propagation on onclick event in html attribute?

From Dev

How to stop function in event handler

From Dev

How to define an OnClick event handler for a button from within Qt Creator?

From Dev

How to stop javascript onclick event

From Dev

How to stop video-js onclick event from pausing video

From Dev

C# How to use an Event Handler from multiple objects?

From Dev

how to stop handler from executing

From Dev

How to set Button onClick event handler declaratively?

From Dev

How to stop onclick from firing before ondblclick in JS/HTML

From Dev

How to prepend html into on page element from HTML onClick event?

From Dev

Pass Multiple arguments onMouseover event handler in HTML

From Dev

onclick event handler not work

From Dev

Form OnClick event handler - How to overlap other event handlers?

From Dev

Nested jQuery onClick event handler fires multiple times

From Dev

Should I stop using HTML event handler attributes

From Dev

stop bubbling event onclick

From Dev

How to stop OCaml garbage collecting my reactive event handler?

From Dev

How to stop OCaml garbage collecting my reactive event handler?

From Dev

Stop propagation through html's "onclick" event and JS

From Dev

Stop propagation through html's "onclick" event and JS

From Dev

How to add an onClick event handler to a canvas shape in Elm?

From Dev

Stop event propagation on click handler

From Dev

OnClick event handler on mobile devices?

From Dev

Onclick event handler seems not called

From Dev

Onclick event handler seems not called

From Dev

How to remove event one handler from view?

From Dev

How can I trigger an angularjs event handler from another handler?

From Dev

HTML: Multiple onclick Audio files, make the last one stop

Related Related

  1. 1

    how to stop execution of click event from onchange event handler

  2. 2

    How to stop event propagation on onclick event in html attribute?

  3. 3

    How to stop function in event handler

  4. 4

    How to define an OnClick event handler for a button from within Qt Creator?

  5. 5

    How to stop javascript onclick event

  6. 6

    How to stop video-js onclick event from pausing video

  7. 7

    C# How to use an Event Handler from multiple objects?

  8. 8

    how to stop handler from executing

  9. 9

    How to set Button onClick event handler declaratively?

  10. 10

    How to stop onclick from firing before ondblclick in JS/HTML

  11. 11

    How to prepend html into on page element from HTML onClick event?

  12. 12

    Pass Multiple arguments onMouseover event handler in HTML

  13. 13

    onclick event handler not work

  14. 14

    Form OnClick event handler - How to overlap other event handlers?

  15. 15

    Nested jQuery onClick event handler fires multiple times

  16. 16

    Should I stop using HTML event handler attributes

  17. 17

    stop bubbling event onclick

  18. 18

    How to stop OCaml garbage collecting my reactive event handler?

  19. 19

    How to stop OCaml garbage collecting my reactive event handler?

  20. 20

    Stop propagation through html's "onclick" event and JS

  21. 21

    Stop propagation through html's "onclick" event and JS

  22. 22

    How to add an onClick event handler to a canvas shape in Elm?

  23. 23

    Stop event propagation on click handler

  24. 24

    OnClick event handler on mobile devices?

  25. 25

    Onclick event handler seems not called

  26. 26

    Onclick event handler seems not called

  27. 27

    How to remove event one handler from view?

  28. 28

    How can I trigger an angularjs event handler from another handler?

  29. 29

    HTML: Multiple onclick Audio files, make the last one stop

HotTag

Archive