Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onclick="return confirm('')" working with $('.class').click(function () {});

I have this function that disables the input after user clicks:

$('.click-off').click(function () {
    var btn = this;
    setTimeout(function () { $(btn).attr('disabled', 'disabled'); }, 1);
    return true;
});

And I have this input with a javascript confirmation:

<input type="submit" class="click-off" onclick="return confirm('Are you sure?');" value="Delete">

Even if I click "cancel" in the confirmation box, the $('.click-off').click() event is called, and the button becomes disabled.

The $('.click-off').click() is used by a lot of inputs, so the confirmation can't be inside of it.

How can I check the confirm return in the $('.click-off').click event? Or even prevent the $('.click-off').click event to be called?

like image 276
Fernando Avatar asked Dec 27 '12 17:12

Fernando


People also ask

What is return false in Onclick?

using return false in an onclick event stops the browser from processing the rest of the execution stack, which includes following the link in the href attribute. In other words, adding return false stops the href from working.

What is onclick property?

The onclick event generally occurs when the user clicks on an element. It allows the programmer to execute a JavaScript's function when an element gets clicked. This event can be used for validating a form, warning messages and many more. Using JavaScript, this event can be dynamically added to any element.

What is window onclick?

onclick runs the function immediately. JavaScript.

Which one of the following events can be used to display a confirmation box when a user submits a form?

onclick Event: This event occurs when the user clicks on an element.


2 Answers

Why would you have these pieces of logic be separate in the first place? Here are 2 methods to get around the problem:

Combine the logic into a single method:

$('.click-off').click(function () {
    // escape here if the confirm is false;
    if (!confirm('Are you sure?')) return false;
    var btn = this;
    setTimeout(function () { $(btn).attr('disabled', 'disabled'); }, 1);
    return true;
});

Use a global variable (or object preferably):

var clickOffConfirmed = false;

<input type="submit" class="click-off" onclick="clickOffConfirmed = confirm('Are you sure?');" value="Delete" />


$('.click-off').click(function () {
    // escape here if the confirm is false;
    if (!clickOffConfirmed) return false;
    var btn = this;
    setTimeout(function () { $(btn).attr('disabled', 'disabled'); }, 1);
    return true;
});
like image 157
Joel Etherton Avatar answered Sep 24 '22 02:09

Joel Etherton


Try capturing the confirm box then disabling the button.

$('.click-off').click(function () {
  var r=confirm("Press a button");
  if (r==true)
  {
    $(this).attr('disabled', 'disabled');
  }
  else
  {

  }
});

also remove the onclick

like image 36
Pedro Estrada Avatar answered Sep 25 '22 02:09

Pedro Estrada