9

I have this sample code here http://jsfiddle.net/DBBUL/10/

    $(document).ready(function ($) {

    $('.creategene').click(function () {

        $('#confirmCreateModal').modal();

        $('#confirmCreateYes').click(function () {
            $('#confirmCreateModal').modal('hide');

            var test = "123";
            alert(test);
            console.log(test);             
        });
    });
});

If you click the create button 3 times and each time you click yes on the confirmation, the alert is fired multiple times for each click instead of just one time.

If you click the create button 3 times and each time you click no and on the 4th time you click yes the alert is fired for each of the previous clicks instead of just one time.

this behavior seems weird to me as i would expect the alert to be fired once per click. Do I have to use .unbind() or is there a better solution?

Could someone please tell me why this is happening and how to fix it?

Thanks

0

5 Answers 5

18

Because you are binding it multiple times. Click event inside a click event means every time you click, a new click event is being bound on top of the previously bound events. Do not bind click events inside of click events unless the click event creates the element. There's also no need to re-initialize the modal over and over.

$(document).ready(function ($) {

    $('#confirmCreateModal').modal({show: false});

    $('#confirmCreateYes').click(function () {
        $('#confirmCreateModal').modal('hide');

        var test = "123";
        alert(test);
        console.log(test);             
    });

    $('.creategene').click(function () {

        $('#confirmCreateModal').modal('show');

    });
});

Demo

Sign up to request clarification or add additional context in comments.

3 Comments

how would you handle this if the main click event (.creategene) occurs in a knockoutjs click binding self.createGene = function () { }
I would have done this using data attributes rather than javascript, then bound to the close event.
well, not the close event, but just the "Yes" button. There's no real need to initialize the modal or have a click event to open it.
2

change the code like this

$(document).ready(function ($) {

    $('.creategene').click(function () {

        $('#confirmCreateModal').modal();


    });
    $('#confirmCreateYes').click(function () {
            $('#confirmCreateModal').modal('hide');

            var test = "123";
            alert(test);
            console.log(test);             
        });
});

fiddle

You dont have to bind $('#confirmCreateYes').click in each button click.

Comments

2

You can try this also -

$("#confirmCreateYes").unbind().click(function() {
//Stuff
});

1 Comment

A code block alone does not provide a good answer. Please add explanations (why it solve the issue, where was the mistake, etc...)
1

Add this to your code:

$( "#confirmCreateYes").unbind( "click" );

Like this:

$(document).ready(function ($) {

$('.creategene').click(function () {

    $('#confirmCreateModal').modal();

    $('#confirmCreateYes').click(function () {
        $('#confirmCreateModal').modal('hide');

        var test = "123";
        alert(test);
        console.log(test);
        $( "#confirmCreateYes").unbind( "click" );
    });
});

});

It will unbind the event, so that it isn't bound on top of the previous event. This allows the event only to fire within the original click event.

This is not a good method. It will unbind all click events attached to the element. I will leave it here for learning purposes.

3 Comments

This isn't going to work. If this approach is chosen, the unbind would need to be before the bind, not inside the function implementation(?). But still this is a poor approach for multiple reasons, the first being that you are blindly unbinding all click events attached to this item, not necessarily just the one in question.
No, it makes more sense for it to be where @kylealonius put it than anywhere else. It will be unbound at the earliest possible point. Though, it would make more sense to further reduce that down to using .one() rather than .click() so that you don't even have to unbind it yourself. But then you get to, why unbind it at all? It can only be triggered while the modal is visible anyway.
Robert is correct about unbinding all click events. I had not thought of that. I was trying to preserve the original design. But since the create yes button has an ID, the other approaches are much better since the click event will not fire for any other element.
0

As of now its the below code worked for me,

$("#parentID").off("click", ".button").on("click", ".button", function(e){
     e.preventDefault(); // stop probagation if its button or a href
     // your code here  
 });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.