To change button text using JavaScript, the simplest way is to use the textContent property along with the getElementById method.

document.getElementById("button").textContent="Submit";

You can also use the innerHTML property to change button text.

document.getElementById("button").innerHTML = "Submit";

Let’s say I have the following HTML form:




If I want to change the button text from “Send” to “Submit”, I can use the textContent property to do this with the following JavaScript code.

document.getElementById("button1").textContent="Submit";

We can also use the innerHTML property to change the content of the button.

document.getElementById("button1").innerHTML="Submit";

While the textContent property is very simple, the innerHTML property gives us the ability to insert HTML into our button, which gives us more options when styling the content. In this case, with buttons, using the innerHTML property probably wouldn’t make much sense.

Changing Button Text Using JavaScript with a Click

To change button text using JavaScript, we can combine the textContent property with a click event.

Let’s say I have the following HTML form, and I want to give the user the ability to change the button text from “Send” to “Submit”.

Click here to change button text



We can utilize both the onclick event and textContent property to change the button text from “Send” to “Submit”.

Below is the JavaScript code which will allow the user to be able to update the text in the submit button:

function updateButton() {
    document.getElementById("button1").textContent="Submit";
};

The final code and output for this example of how to change the text of a submit button using JavaScript is below:

Code Output:

Click here to change button text



Full Code:

Click here to change button text




<script>

function updateButton() {
    document.getElementById("button1").textContent="Submit";
};

</script>

Hopefully this article has been useful for you to understand how to use JavaScript to change button text.

Categorized in:

JavaScript,

Last Update: May 3, 2024