Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disabled button still fires using ".click()"

It seems disabled button "onclick" function is still fired when triggering it programmaticaly, eg:

<div>
   <input type="button" onclick="save()" id="saveButton"    value="save"  disabled="disabled" />
   <input type="button" onclick="byPassDisabled()" value="bypass disabled button"/>
<div id="counter">0</div>

function save(){
    var count = parseInt($('#counter').html());
    $('#counter').html(++count);
}
function byPassDisabled(){
     $('#saveButton').click();   
}

see http://jsfiddle.net/WzEvs/363/

In my situation, keyboards shortcuts are bound to functions triggering the ".click()" on buttons. I'll find it very annoying to have to disable the shorcuts or check if the button is disabled myself. I'd prefer a general solution fixing this problem.

  • But why? This behavior doesn't seem fair to me.
  • Any workaround?
like image 781
domi Avatar asked Jan 29 '15 11:01

domi


People also ask

Why disabled Button is still clickable?

Because you have to enable click event again, if you enable the button.

Does click event fire on disabled button?

I want to fire the onclick event of the button. It is not possible to fire Click for disabled button. Simply add a Hidden Button (style="display:none") and Fire click.

How do I disable a clicking button?

To disable a submit button, you just need to add a disabled attribute to the submit button. $("#btnSubmit"). attr("disabled", true); To enable a disabled button, set the disabled attribute to false, or remove the disabled attribute.


1 Answers

The attribute only disables user interaction, the button is still usable programmatically.

So yeah, you gotta check

function byPassDisabled(){
    $('#saveButton:enabled').click();   
}

Alternatively don't use inline handlers.

$(document).on('click', '#saveButton:enabled', function(){
    // ...
});
like image 185
Spokey Avatar answered Oct 17 '22 17:10

Spokey