Categories
JavaScript Answers

How to Set a Button’s Value Using JavaScript?

Spread the love

To set a button’s value using JavaScript, we can set the innerHTML property of the button.

For instance, if we have the following HTML:

<form>
  <button name="submit-button">Original<br>Text</button>
</form>

We can write:

const form = document.querySelector('form')
form.elements["submit-button"].innerHTML = 'submit'

to get the form with document.querySelector .

Then we get the button with the name attribute set to submit-button with:

form.elements["submit-button"]

And we set the innerHTML property of it to 'submit' .

Therefore, the button in the form would have ‘submit’ as its text content.

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 *