How to Change the Button Label when Clicked using JavaScript ?
Last Updated :
28 Apr, 2025
Changing the Label of the Button element when clicked in JavaScript can be used to provide more information to the user such as the text of the Submit button will change to the Submitted as soon as the form submission is completed.
The below approaches can be used to accomplish this task:
Changing Button Label Using innerHTML property
The innerHTML property in JavaScript is used to dynamically change the content inside any element. The changeLabel function, which is triggered by the button's onclick attribute, selects the button and updates its innerHTML, changing the button label.
Syntax:
selctedHTMLElement.innerHTML = "contentToAppend";
Example: The below example uses innerHTML property to change the button label when clicked in JavaScript.
HTML
<!DOCTYPE html>
<html>
<head>
<title>
Change Button Label
</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
margin: 40px;
}
h1 {
color: green;
}
h3 {
color: #333;
}
button {
padding: 10px 20px;
font-size: 16px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
button:hover {
background-color: #45a049;
}
</style>
</head>
<body>
<h1>GeeksforGeeks</h1>
<h3>
Using innerHTML property
</h3>
<button onclick="changeLabel()">
Click me
</button>
<script>
function changeLabel() {
const button =
document.querySelector('button');
button.innerHTML = 'Button Clicked!';
}
</script>
</body>
</html>
Output:

Changing Button Label Using innerText property
The innerText property in JavaScript is used to dynamically modify the text content within any element. The toggleLabel function, triggered by the button's onclick event, checks the current label and toggles between two states, changing the button's text.
Syntax:
selectedHTMLElement.innerText = text
Example: The below example uses innerText property to change the button label when clicked in JavaScript.
HTML
<!DOCTYPE html>
<html>
<head>
<title>
Button Label
</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
margin: 40px;
}
button {
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
}
h1 {
color: green;
}
</style>
</head>
<body>
<h1>GeeksforGeeks</h1>
<h3>Using innerText property</h3>
<button onclick="toggleLabel()">
Click me
</button>
<script>
function toggleLabel() {
const btn =
document.querySelector('button');
if (btn.innerText === 'Click me') {
btn.innerText = 'Label Changed!';
} else {
btn.innerText = 'Click me';
}
}
</script>
</body>
</html>
Output:

Similar Reads
How to change a button text on click using localStorage in Javascript ? In this article, we will learn how to change a button text using Javascript localStorage when we click on the button achieved by adding HTML onclick event listener. Approach: HTML DOM Window localStorage allows us to store key-value pairs in our browser using an object. The name of the key should be
3 min read
How to change the text of a label using JavaScript ? Changing the text of a label using JavaScript involves selecting the label element and updating its textContent or innerHTML property. This allows you to modify the displayed text dynamically, often in response to user interactions or changes in data, enhancing interactivity and responsiveness.Below
2 min read
How to change the href value of a tag after click on button using JavaScript ? JavaScript is a high-level, interpreted, dynamically typed, and client-side scripting language. HTML is used to create static web pages. JavaScript enables interactive web pages when used along with HTML and CSS. Document Object Manipulation (DOM) is a programming interface for HTML and XML document
4 min read
How to Change Button Label in Alert Box using JavaScript ? In JavaScript, the alert method is used to display an alert box with a message. By default, the alert box contains an "OK" button. We cannot directly update the default alert box, However, we can customize the button label by creating a custom alert box using HTML, CSS, and JavaScript. ApproachCreat
2 min read
How to get the ID of the clicked button using JavaScript/jQuery ? To get the ID of a clicked button using JavaScript/jQuery means identifying which specific button was clicked within a webpage. This is useful when multiple buttons exist, and you need to distinguish between them based on their unique ID for further processing.So, to get the ID of the clicked button
3 min read