So I have a button like this:
<input id="Button" type="button" value="+" style="background-color:grey" onclick="Me();"/>
How can I disable and enable it when I want? I have tried disabled="disable"
but enabling it back is a problem. I tried setting it back to false but that didn't enable it.
You can disable the <button> element in HTML by adding the disabled attribute to the element. The disabled attribute is a boolean attribute that allows you to disable an element, making the element unusable from the browser.
A disabled input element is unusable and un-clickable. The disabled attribute can be set to keep a user from using the <input> element until some other condition has been met (like selecting a checkbox, etc.). Then, a JavaScript could remove the disabled value, and make the <input> element usable.
Setting the property to true will enable the button (clickable) and setting it false (like bt. disabled = false;) will disable the button (un-clickable).
The <button> tag defines a clickable button. Inside a <button> element you can put text (and tags like <i> , <b> , <strong> , <br> , <img> , etc.). That is not possible with a button created with the <input> element!
Disabling a html button
document.getElementById("Button").disabled = true;
Enabling a html button
document.getElementById("Button").disabled = false;
Demo Here
All versions of jQuery prior to 1.6
Disabling a html button
$('#Button').attr('disabled','disabled');
Enabling a html button
$('#Button').removeAttr('disabled');
Demo Here
All versions of jQuery after 1.6
Disabling a html button
$('#Button').prop('disabled', true);
Enabling a html button
$('#Button').prop('disabled', false);
Demo Here
P.S. Updated the code based on jquery 1.6.1 changes. As a suggestion, always use the latest jquery files and the prop()
method.
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