How to Disable Button in Javascript

In this article, you will learn how to disable a button in Javascript. Let’s say you have a button element with id “theButton”. html In…

The post How to Disable Button in Javascript appeared first on CodeSource.io.


This content originally appeared on CodeSource.io and was authored by Ariessa Norramli

In this article, you will learn how to disable a button in Javascript.

Let’s say you have a button element with id “theButton”.

html

<button id="theButton">Hi, I'm a button</button>

In order to disable the button, you can use the document.getElementById() method and button disabled property, button.disabled.

full html

<!-- A button element with id "theButton" -->
<button id="theButton">Hi, I'm a button</button>

<!-- A button element that executes "disableButton" function upon clicking -->
<button onclick="disableButton()">Disable</button>

full javascript

// A function that disables button
function disableButton() {
  // Select the element with id "theButton" and disable it
  document.getElementById("theButton").disabled = true;
}

Before disabling button

After disabling button

Note: The document.getElementById() method functions by getting an element whose id matches the supplied string. The button disabled property, button.disabled functions by preventing the user from interacting with the button.

The post How to Disable Button in Javascript appeared first on CodeSource.io.


This content originally appeared on CodeSource.io and was authored by Ariessa Norramli


Print Share Comment Cite Upload Translate Updates
APA

Ariessa Norramli | Sciencx (2021-02-20T11:35:03+00:00) How to Disable Button in Javascript. Retrieved from https://www.scien.cx/2021/02/20/how-to-disable-button-in-javascript/

MLA
" » How to Disable Button in Javascript." Ariessa Norramli | Sciencx - Saturday February 20, 2021, https://www.scien.cx/2021/02/20/how-to-disable-button-in-javascript/
HARVARD
Ariessa Norramli | Sciencx Saturday February 20, 2021 » How to Disable Button in Javascript., viewed ,<https://www.scien.cx/2021/02/20/how-to-disable-button-in-javascript/>
VANCOUVER
Ariessa Norramli | Sciencx - » How to Disable Button in Javascript. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/02/20/how-to-disable-button-in-javascript/
CHICAGO
" » How to Disable Button in Javascript." Ariessa Norramli | Sciencx - Accessed . https://www.scien.cx/2021/02/20/how-to-disable-button-in-javascript/
IEEE
" » How to Disable Button in Javascript." Ariessa Norramli | Sciencx [Online]. Available: https://www.scien.cx/2021/02/20/how-to-disable-button-in-javascript/. [Accessed: ]
rf:citation
» How to Disable Button in Javascript | Ariessa Norramli | Sciencx | https://www.scien.cx/2021/02/20/how-to-disable-button-in-javascript/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.