Jack Roscoe Jack Roscoe - 8 months ago 25
HTML Question

How to disable html button using JavaScript?

I've read that you can disable (make physically unclickable) a html button simply but appending 'disable' to its tag, but not as an attribute, as follows:

<input type="button" name=myButton value="disable" disabled>

Since this setting is not an attribute, how can I add this in dynamically via JavaScript to disable a button that was previously enabled?


Since this setting is not an attribute

It is an attribute.

Some attributes are defined as boolean, which means you can specify their value and leave everything else out. i.e. Instead of disabled="disabled", you include only the bold part. In HTML 4, you should include only the bold part as the full version is marked as a feature with limited support (although that is less true now then when the spec was written).

The DOM property is also called disabled and is a boolean that takes true or false.

foo.disabled = true;

In theory you can also foo.setAttribute('disabled', 'disabled');, but I wouldn't trust this with Internet Explorer (which is notoriously buggy when it comes to setAttribute).