Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disabling and enabling a html input button

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.

like image 371
k.ken Avatar asked Dec 12 '12 01:12

k.ken


People also ask

Can we disable button in HTML?

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.

How do you create a disabled input in HTML?

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.

What is enable and disable button based on condition in HTML?

Setting the property to true will enable the button (clickable) and setting it false (like bt. disabled = false;) will disable the button (un-clickable).

How do I activate a button in HTML?

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!


1 Answers

Using Javascript

  • Disabling a html button

    document.getElementById("Button").disabled = true; 
  • Enabling a html button

    document.getElementById("Button").disabled = false; 
  • Demo Here


Using jQuery

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.

like image 90
palaѕн Avatar answered Oct 21 '22 15:10

palaѕн