Welcome Guest, Not a member yet? Register   Sign In
problem with the confirmation before delete using javascript
#2

[eluser]Frank Berger[/eluser]
try this:

Code:
<js>
$(document).ready(function() {
  $('.confirmClick').live('click',function() {
    var q = 'Are you sure you want to do this action?';
    var t = $(this).attr('title');
    if (t.length > 0) q = 'Are you sure you want to ' + t.toLowerCase() + '?';
    var c = confirm(q);
    return c;
  });
});
<js>

what happens is that confirm returns true or false, and you return that to the click-event. If its true, the click will be executed (no need for a window-location-href), if it is false the click will be aborted.

If you don't use (for whatever reason) jQuery 1.3+, then replace the .live('click',function with .click(function . the live method ensures that even if the link is added to the content after the event has been bound that it will apply to this link (like if the link has been loaded through ajax or been added with jquery in any other way.

Additionally, I always let the confirm return into a variable, I had weird race-conditions before when I had the confirm inside an if-statement + it is not really needed (the if-statement) in this case.

The title is read into a var first, because your statement would be true otherwise even if the title is empty (like <a href="bla" title="">xx</a> ) this way it is checked for real content.

I wrote above from my head, but i am pretty sure it'll work

cheers
Frank


Messages In This Thread
problem with the confirmation before delete using javascript - by El Forum - 05-23-2009, 10:04 AM



Theme © iAndrew 2016 - Forum software by © MyBB