views:

160

answers:

3

Hi,

I'm having problems with .live() and confirm. It's multiplying the confirm dialogs for every click. I know about .die() but i can't get it to work.

$("button.del").live("click", function(){

        if(!confirm("Are you sure?")) {
            //close
        }

    });

I've tried $("button.del").die("click"); right after the above code, in which case the confim doesn't even fire.

+1  A: 

Does the dialog box appear multiple times if you just run that code by itself?

If the dialog box is appearing multiple times, one likely explanation is that you are accidentally running this .live() binding more than once. If that happened, you would see one dialog box for each time you bound an event to the button.

Make sure you are only attaching this function to the button once.

If you take a look at this standalone example, you can see that your code is fine.

Ryan
+1  A: 

Can you post the HTML as well.

One cause I can speculate for this is that the .del class is specified into some child class, and the event is firing on both parent and child. This would happen for the following:

<div class="testclass">
    test
    <div class="testclass">
        test2
    </div>
</div>

...

$(".testclass").click(function() { alert("test"); });

Another reason would be if you accidentally bound it twice, i.e. the following would cause the same problem

$(".testclass").click(function() { alert("test"); });
$(".testclass").click(function() { alert("test"); });

We really need to see more of your code. You must utilise live for a reason. Do you get the same result with a simple click() binding?

James Wiseman
He said it's multiplying after every click, so I don't think he's having issues with parents.
Ondrej Slinták
+1  A: 

Thank you all for your replies...turn out it was a bug in my code... Sorry...

I didn't see it... the part of the code with confirm was reloading on every click...hence the multiplying...

Mission