My page creates multiple buttons as id = 'rbutton_"+i+"'
. Below is my code:
<button type='button' id = 'rbutton_"+i+"' onclick=disable(i);>Click me</button>
In Javascript
function disable(i){ $("#rbutton'+i+'").attr("disabled","disabled"); }
But it doesn't disable my button when I click on it.
The disabled attribute is a boolean attribute. When present, it specifies that the button should be disabled. A disabled button is unusable and un-clickable. The disabled attribute can be set to keep a user from clicking on the button until some other condition has been met (like selecting a checkbox, etc.).
click(function () { if ($('#submit-button').is(':disabled')) { $('#submit-button'). removeAttr('disabled'); } else { $('#submit-button'). attr('disabled', 'disabled'); } }); In the above code, enabled is the id of the checkbox used and submit-button is the class name of the submit button used .
Use .prop
instead (and clean up your selector string):
function disable(i){ $("#rbutton_"+i).prop("disabled",true); }
generated HTML:
<button id="rbutton_1" onclick="disable(1)">Click me</button> <!-- wrap your onclick in quotes -->
But the "best practices" approach is to use JavaScript event binding and this
instead:
$('.rbutton').on('click',function() { $(this).prop("disabled",true); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <button class="rbutton">Click me</button>
http://jsfiddle.net/mblase75/2Nfu4/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With