How to Change a Button's Text on Click in JavaScript
Changing the text of a button after a user clicks it is a fundamental UI interaction. It provides clear feedback that an action has been taken, such as changing from "Submit" to "Submitting..." or "Play" to "Pause."
This guide will teach you the standard method for changing a button's text using a click event listener. You will learn the important difference between using textContent for plain text and innerHTML for HTML content, and then see how to "toggle" the button's text back and forth between two states on each click.
Core Task: Handling a Click Event
The first step is always to select the button element from the DOM and attach a click event listener to it. This listener will execute a function whenever the user clicks the button.
First, we need a button in our HTML with a unique ID so we can select it.
<!DOCTYPE html>
<html>
<body>
<button id="action-button">Click me</button>
<script src="index.js"></script>
</body>
</html>
// 1. Select the button from the DOM
const btn = document.getElementById('action-button');
// 2. Add a 'click' event listener
btn.addEventListener('click', function() {
// 3. This code will run every time the button is clicked
console.log('Button was clicked!');
});
Solution 1: Change the Text on the First Click
Once the event listener is set up, changing the button's text is as simple as setting its textContent property to a new string.
const btn = document.getElementById('action-button');
btn.addEventListener('click', function handleClick() {
// Set the button's text content to a new value
btn.textContent = 'Button clicked!';
});
When the user clicks the button, its text will instantly change. Subsequent clicks won't have any effect because the text is already set to the new value.
textContent vs. innerHTML: An Important Distinction
JavaScript gives you two main properties for changing an element's content, and it's crucial to know the difference.
textContent: This property gets or sets the plain text content of an element. It automatically ignores any HTML tags, making it fast and secure. This is the recommended method for changing simple text.innerHTML: This property gets or sets the full HTML content inside an element. It allows you to add other HTML elements, like<span>s or<i>cons.
Use innerHTML when you need to include HTML tags, for example, to add an icon or a loading spinner.
btn.addEventListener('click', function handleClick() {
btn.innerHTML = '<em>Processing...</em>';
});
Security Warning: Never set innerHTML with a value that comes from user input without sanitizing it first. Doing so can expose your site to Cross-Site Scripting (XSS) attacks. For plain text, always prefer textContent.
Solution 2: Toggle the Button Text on Every Click
To make the button's text switch back and forth between two states (e.g., "Play" and "Pause"), you can add a simple conditional check inside your event handler.
The logic:
- Define the two text states (e.g., "initial" and "clicked").
- Inside the click handler, check the button's current
textContent. - If it matches the initial text, change it to the "clicked" text.
- Otherwise, change it back to the initial text.
const btn = document.getElementById('action-button');
const initialText = 'Play';
const toggledText = 'Pause';
// Set the initial text
btn.textContent = initialText;
btn.addEventListener('click', function handleClick() {
if (btn.textContent === initialText) {
btn.textContent = toggledText;
} else {
btn.textContent = initialText;
}
});
With this code, each click will toggle the button's text between "Play" and "Pause".
Conclusion
Changing a button's text on click is a fundamental UI programming task that is easily accomplished in JavaScript.
To do it successfully:
- Select the button from the DOM using a method like
document.getElementById(). - Add a click event listener with
addEventListener('click', function() { ... }). - Inside the handler function, set the
.textContentproperty to change the button's plain text. - Use the
.innerHTMLproperty only when you need to add HTML elements inside the button. - For toggling text, use an
if/elsestatement inside the handler to check the current text and set it to the alternative.