Last updated: Mar 5, 2024
Reading time·3 min
To change the text of a button on click:
click
event listener to the button.textContent
property to change the button's text.btn.textContent = 'Button clicked'
.Here is the HTML for the examples.
<!DOCTYPE html> <html lang="en"> <head> <title>bobbyhadz.com</title> <meta charset="UTF-8" /> </head> <body> <button id="btn">Click me</button> <script src="index.js"></script> </body> </html>
And here is the related JavaScript code.
const btn = document.getElementById('btn'); // ✅ Change button text on click btn.addEventListener('click', function handleClick() { btn.textContent = 'Button clicked'; }); /** * ✅ If you need to change the button's inner HTML use: * - `innerHTML` instead of `textContent` */
We added a click
event listener to the button.
Every time the button is clicked, the handleClick
function is invoked.
The textContent property can be used to change the button's text every time it's clicked.
textContent
property represents the text content of the node and its descendants.If you need to change the button's inner HTML, use the innerHTML
property
instead of textContent
.
const btn = document.getElementById('btn'); // ✅ Change button text on click btn.addEventListener('click', function handleClick() { const initialText = 'Click me'; btn.innerHTML = `<span style="background-color: salmon">Button clicked<span>`; });
This approach could be used to add a loading spinner when the user clicks a button.
innerHTML
based on user-provided input without escaping it. This would make your site vulnerable to cross-site scripting attacks.To toggle a button's text on click:
click
event listener to the button.const btn = document.getElementById('btn'); // ✅ Toggle button text on click btn.addEventListener('click', function handleClick() { const initialText = 'Click me'; if (btn.textContent.toLowerCase().includes(initialText.toLowerCase())) { btn.textContent = 'Button clicked'; } else { btn.textContent = initialText; } }); /** * ✅ If you need to change the button's inner HTML use: * - `innerHTML` instead of `textContent` */
Each time the button is clicked, the handleClick
function is invoked.
In the function, we check if the button's textContent
contains the initial
text value.
initialText
and the button's text content to lowercase to make a case-insensitive comparison.If the button's text is the initial text, we set the text to a new value of
Button clicked
.
Otherwise, we set the button's text to the initial text.
If you need to use HTML when setting the button's content, use the innerHTML
property instead of textContent
.
const btn = document.getElementById('btn'); // ✅ Toggle button text on click btn.addEventListener('click', function handleClick() { const initialText = 'Click me'; if (btn.textContent.toLowerCase().includes(initialText.toLowerCase())) { btn.innerHTML = '<span style="background-color: lime">Button clicked</span>'; } else { btn.textContent = initialText; } });
We used the innerHTML
property to set the content of a button to a span
with
a background color.
In a more real-world scenario, you would probably set the content to some text and a loading spinner.
You can learn more about the related topics by checking out the following tutorials: