Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

274
Views
Intercept click event on a button, ask for confirmation, then proceed

Based on a variable SomeCondition, I need to intercept a click event on a button, and ask for confirmation, if they say ok, proceed, otherwise ignore click.

So something like:

if(SomeCondition) {

// disable click on button

var isOk = window.confirm("Are you sure?");

if(isOk) {
        $("#button1").click();
}

}

Note: button1 has already been wired up with a click event via javascript from an external .js file that I can't change.

I don't know what the click event was bound too, so I have to disable the click if SomeCondition is true, then ask for confirmation, then continue with the click.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Process isOK your window.confirm within the function of the button

$('#button1').click(function(){ 
   if(window.confirm("Are you sure?"))
     alert('Your action here');
});

The issue you're going to have is the click has already happened when you trigger your "Are You Sure" Calling preventDefault doesn't stop the execution of the original click if it's the one that launched your original window.confirm.

Bit of a chicken/egg problem.

Edit: after reading your edited question:

    var myClick = null;

    //get a list of jQuery handlers bound to the click event
    var jQueryHandlers = $('#button1').data('events').click;

    //grab the first jquery function bound to this event
    $.each(jQueryHandlers,function(i,f) {
       myClick = f.handler; 
       return false; 
    });

    //unbind the original
    $('#button1').unbind('click');

    //bind the modified one
    $('#button1').click(function(){
        if(window.confirm("Are You Sure?")){
            myClick();
        } else {
            return false;
        }
    });
over 4 years ago · Santiago Trujillo Report

0

With jQuery to prevent default you just return false:

$("#button1").click(function () {
    //do your thing
    return false;
});
over 4 years ago · Santiago Trujillo Report

0

To anyone who is looking for a simple solution (not particular to the special condition mentioned here)

Just add this attribute to the button:

.... , onclick = "return confirm('are you sure?')"

When the confirmation box returns a false, the click event gets cancelled. Note: The onclick handler explicitly mentioned inline will override any other bindings.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!