1

I am trying to create an alert, to insure that the user is submitting the correct information and if 'Ok' is clicked rather than cancel, the link is clicked and <a> sent. I have nearly achieved it, the alert activates, however does not activate if ok is clicked. Unfortunately, I am not a js wizard, yet..

EDIT :

click => preventDefault => alert (yes, no) if yes(send) if no dont.

<script>
jQuery(document).ready(function($){

$("input.btn-default").click(function(e){
    e.preventDefault();
    var answer=confirm('Are you sure that you want to do this?');
    if(answer == true){
        ///not sure how to remove the prevent default and send link?!
    }
    else{
       return;
    }
    });
});

</script>
Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101
m33bo
  • 1,334
  • 1
  • 17
  • 34
  • prevent default behaviour in `else` or return false from `else` block. `confirm()` method is modal, so you don't have to worry about queued event. That's said, in `if` block, you could use `$(this).off('click').click();` – A. Wolff Nov 27 '15 at 17:36

2 Answers2

2

Try the following code using return false; if the user cancel confirm dialog and window.open( $(this).att('href') ); to open the link when he user click OK :

$("input.btn-default").click(function(e)
{
    e.preventDefault();

    if( confirm('Are you sure that you want to do this?') )
    {
        window.open( $(this).att('href') );
    }
    else
    {
       return false;
    }
});

Hope this helps.

Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101
  • Or set `e.preventDefault();` in `else` block. `window.open()` could have different behaviour than clicking anchor – A. Wolff Nov 27 '15 at 17:41
  • My answer based on OP description _the link is clicked and sent_. – Zakaria Acharki Nov 27 '15 at 17:42
  • Ya but by clicking on some links, you would open a new tab/window or redirect on same page,depending browser config, user preference, user holding ctrl key, etc.... Now `window.open()` could react differently. EDIT: ok i see what you mean but i guess the question is wrongly asked. OP seems to want to stop preventing default behaviour in fact. EDIT2: of course, i could be completly wrong... but in comment: `///not sure how to remove the prevent default and send link` :) – A. Wolff Nov 27 '15 at 17:45
  • Okay @A.Wolff thanks for your explanation, anyway we have the two cases here let him choose the answer which suits his needs. – Zakaria Acharki Nov 27 '15 at 17:53
  • Sorry my intention was not to create frustration, that was already implied with the question! I have used @A.Wolff previous answer, and the latest worked. Thank you both. – m33bo Nov 27 '15 at 17:56
0

confirm() is modal, so you just need to check for answer:

jQuery(document).ready(function ($) {
    $("input.btn-default").click(function (e) {
        var answer = confirm('Are you sure that you want to do this?');
        if (!answer) {
            e.preventDefault();
        }
    });
});
A. Wolff
  • 74,033
  • 9
  • 94
  • 155
  • I used this ` $("input.btn-default").click(function(e){ e.preventDefault(); var answer=confirm('Are you sure that you want to do this?'); if(answer == true){ $(this).off('click').click(); } else{ return false; } }); }); ` – m33bo Nov 27 '15 at 17:52
  • and it worked, looking at both it works, however I havent tried in varying browsers. – m33bo Nov 27 '15 at 17:52
  • `confirm()` is modal on all browsers, you should instead checking for answer like in this answer because user could hold ctrl or shift key while clicking anchor. Using your fix, this would give different behaviour than expected one – A. Wolff Nov 27 '15 at 17:56
  • I will add to this script later but I will use this http://stackoverflow.com/questions/16190455/how-to-detect-controlclick-in-javascript-from-an-onclick-div-attribute to ensure that if it is clicked with ctrl, it will fire appropriately. Thanks again. – m33bo Nov 27 '15 at 17:59
  • 1
    I am using but I just copied and pasted out of my environment ;) – m33bo Nov 27 '15 at 18:02