Disable button in jQuery

Jquery

Jquery Problem Overview


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.

Jquery Solutions


Solution 1 - Jquery

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/

Solution 2 - Jquery

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>

Solution 3 - Jquery

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

Solution 4 - Jquery

disable button:

$('#button_id').attr('disabled','disabled');

enable button:

$('#button_id').removeAttr('disabled');

Solution 5 - Jquery

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

Solution 6 - Jquery

Try this

function disable(i){
    $("#rbutton_"+i).attr("disabled",true);
}

Solution 7 - Jquery

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.

Solution 8 - Jquery

Here's how you do it with ajax.

$("#updatebtn").click(function () {
    $("#updatebtn").prop("disabled", true);
	urlToHandler = 'update.ashx';
            jsonData = data;
            $.ajax({
                url: urlToHandler,
                data: jsonData,
                dataType: 'json',
                type: 'POST',
                contentType: 'application/json',
                success: function (data) {
                    $("#lbl").html(data.response);
                    $("#updatebtn").prop("disabled", false);
                    //setAutocompleteData(data.response);
                },
                error: function (data, status, jqXHR) {
                    alert('There was an error.');
                    $("#updatebtn").prop("disabled", false);
                }
            }); // end $.ajax

Solution 9 - Jquery

This works for me:

<script type="text/javascript">
function change(){
	document.getElementById("submit").disabled = false;
}
</script>

Solution 10 - Jquery

I want to disable button on some condition, i am using 1st solution but it won't work for me. But when I use 2nd one it worked.Below are outputs from browser console.

$('#psl2 .btn-continue').prop("disabled", true)

<a class=​"btn btn-yellow btn-continue" href=​"#">​Next​</a>​

2. $('#psl2 .btn-continue').attr("disabled","disabled")

<a class=​"btn btn-yellow btn-continue" href=​"#" disabled=​"disabled">​Next​</a>​

Solution 11 - Jquery

For Jquery UI buttons this works :

$("#buttonId").button( "option", "disabled", true | false );

Solution 12 - Jquery

Using arrow functions

$('#button-id').on('click', e => $(e.target).prop('disabled', true));

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser2047817View Question on Stackoverflow
Solution 1 - JqueryBlazemongerView Answer on Stackoverflow
Solution 2 - JqueryLucasView Answer on Stackoverflow
Solution 3 - JqueryAlessandro MinoccheriView Answer on Stackoverflow
Solution 4 - JqueryCrisView Answer on Stackoverflow
Solution 5 - JqueryMorvaelView Answer on Stackoverflow
Solution 6 - JqueryMaryAnnView Answer on Stackoverflow
Solution 7 - JqueryGeorge PradeepView Answer on Stackoverflow
Solution 8 - Jqueryuser890332View Answer on Stackoverflow
Solution 9 - JqueryJavi PsView Answer on Stackoverflow
Solution 10 - JqueryPooja ManeView Answer on Stackoverflow
Solution 11 - JqueryKhrys aka LouisView Answer on Stackoverflow
Solution 12 - JqueryJonView Answer on Stackoverflow