• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / How to Use JavaScript to Change Button Text

How to Use JavaScript to Change Button Text

January 29, 2022 Leave a Comment

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:

<form action="/action_page.php" method="get" id="form1">
<label for="firstname">First Name:</label>
<input type="text" id="fname" name="firstname">
<label for="lastname">Last Name:</label>
<input type="text" id="lname" name="lastname">
</form>

<button id="button1" type="submit" form="form1" value="Submit">Send</button>

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”.


<div id="click-me" onclick="updateButton()">Click here to change button text</div>
<form action="/action_page.php" method="get" id="form1">
<label for="firstname">First Name:</label>
<input type="text" id="fname" name="firstname">
<label for="lastname">Last Name:</label>
<input type="text" id="lname" name="lastname">
</form>

<button id="button1" type="submit" form="form1" value="Submit">Send</button>

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:

<div id="click-me" onclick="updateButton()">Click here to change button text</div>
<form action="/action_page.php" method="get" id="form1">
<label for="firstname">First Name:</label>
<input type="text" id="fname" name="firstname">
<label for="lastname">Last Name:</label>
<input type="text" id="lname" name="lastname">
</form>

<button id="button1" type="submit" form="form1" value="Submit">Send</button>

<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.

Other Articles You'll Also Like:

  • 1.  JavaScript slice – How to Get a Substring From a String
  • 2.  Remove Commas From a String in JavaScript
  • 3.  How to Repeat a String in JavaScript
  • 4.  How to Check if a String Contains Vowels in JavaScript
  • 5.  Using JavaScript to Check if Variable is an Object
  • 6.  Using JavaScript to Convert Double to Integer
  • 7.  Using JavaScript to Check if Variable is a Number
  • 8.  Creating a JavaScript Function to Add Two Numbers
  • 9.  JavaScript Opacity – How to Change the Opacity of an Element Using JavaScript
  • 10.  Using JavaScript to Reverse a Number

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy