Categories
JavaScript Answers

How to disable all input buttons with JavaScript?

Spread the love

Sometimes, we want to disable all input buttons with JavaScript.

In this article, we’ll look at how to disable all input buttons with JavaScript.

How to disable all input buttons with JavaScript?

To disable all input buttons with JavaScript, we can select all the input elements and loop through them to set the disabled property of each to true.

For instance, we write:

<input type='button' value='button'>
<input type='button' value='button'>
<input type='button' value='button'>

to add a few input elements.

Then we write:

const inputs = document.getElementsByTagName("input");
for (const input of inputs) {
  input.disabled = true
}

to select all the inputs with getElementsByTagname.

Next, we loop through each input with the for-of loop.

In the loop body, we set the disabled property of each input to true to disable them.

Conclusion

To disable all input buttons with JavaScript, we can select all the input elements and loop through them to set the disabled property of each to true.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *