View Tutorial Metadata Edit Content Revision History Add New Tutorial Add to Watchlist How to enable or disable an input element with jQuery

How to enable/disable an input element with jQuery

If you want to disable an element with pure HTML then you would add a property called disabled :

<input type="submit" name="submit" id="submit_btn" disabled="disabled" />

but if you want to do this dynamically you can use Javascript.

An example where this is needed : you perform an AJAX request and you don't want the user to be able to do several clicks on the button that triggers the request.

A simple implementation would be .. disable the button before triggering the AJAX request and enable the button after your call successfully ended.

Here is the jQuery code for enabling / disabling the button from the code above :

// Disable button
$("#submit_btn").attr("disabled", true); 
// Enable button
$("#submit_btn").removeAttr("disabled");

Talk icons discuss – in the article above, point your mouse over them to reply.
Can't find a relevant Talk icon for what you have to say? See how you can add one...
Note: All the talks and replies are also listed below.


Add a new discussion topic (Only if you don't find it above, duplicates will be removed)


Rating: (0+, 0-) In: jQuery