My page creates multiple buttons as id = \'rbutton_\"+i+\"\'
. Below is my code:
Try this code:
HTML
<button type='button' id = 'rbutton_'+i onclick="disable(i)">Click me</button>
function
function disable(i){
$("#rbutton_"+i).attr("disabled","disabled");
}
Other solution with jquery
$('button').click(function(){
$(this).attr("disabled","disabled");
});
DEMO
Other solution with pure javascript
<button type='button' id = 'rbutton_1' onclick="disable(1)">Click me</button>
<script>
function disable(i){
document.getElementById("rbutton_"+i).setAttribute("disabled","disabled");
}
</script>
DEMO2
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/
There are two things here, and the highest voted answer is technically correct as per the OPs question.
Briefly summarized as:
$("some sort of selector").prop("disabled", true | false);
However should you be using jQuery UI (I know the OP wasn't but some people arriving here might be) then while this will disable the buttons click event it wont make the button appear disabled as per the UI styling.
If you are using a jQuery UI styled button then it should be enabled / disabled via:
$("some sort of selector").button("enable" | "disable");
http://api.jqueryui.com/button/#method-disable
Simply it's work fine, in HTML:
<button type="button" id="btn_CommitAll"class="btn_CommitAll">save</button>
In JQuery side put this function for disable button:
function disableButton() {
$('.btn_CommitAll').prop("disabled", true);
}
For enable button:
function enableButton() {
$('.btn_CommitAll').prop("disabled", false);
}
That's all.
This is the simplest way in my opinion:
// All buttons where id contains 'rbutton_'
const $buttons = $("button[id*='rbutton_']");
//Selected button onclick
$buttons.click(function() {
$(this).prop('disabled', true); //disable clicked button
});
//Enable button onclick
$('#enable').click(() =>
$buttons.prop('disabled', false) //enable all buttons
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="rbutton_200">click</button>
<button id="rbutton_201">click</button>
<button id="rbutton_202">click</button>
<button id="rbutton_203">click</button>
<button id="rbutton_204">click</button>
<button id="rbutton_205">click</button>
<button id="enable">enable</button>