How to change button text JavaScript?

In this article, you are going to learn to change the button text in JavaScript. Also, we will discuss the different methods to get this functionality with JavaScript, offering you with step-by-step instructions and real-life examples.

How to change text on button click in JavaScript?

In this part, we will explore different ways to change button text using JavaScript.

Each method comes with its own advantages and uses cases, providing you the flexibility to select the one that fits your specific requirements.

InnerHTML Manipulation

One of the easiest methods to change button text is by performing the innerHTML property of the button element.

Here’s an example code:

<button id="btnValue">Login</button>

const createButton = document.getElementById('btnValue');
button.innerHTML = 'Add Value';

TextContent Modification

A different way is to use the textContent property to change the button text:

Let’s see an example code:

const createButton = document.getElementById('btnValue');
button.innerHTML = 'Update Value';

CreateTextNode Method

The createTextNode method enables you to make a new text node and replace the existing button text with it:

const createButton = document.getElementById('btnValue');
const addValue = document.createTextNode('Change Value');
createButton.innerHTML = '';
createButton.appendChild(addValue);

Button Value Change

Another method to change the button text is by updating its value attribute:

const createButton = document.getElementById('btnValue');
button.value = 'Changed Value';

Using JQuery

If your project includes jQuery, you can use its simplicity to change button text:

$('#btnValue').text('Modified Value');

CSS Class Replacement

This method required creating different CSS classes, each with distinct button text, and then swapping classes using JavaScript:

In your index.html:

<button id="btnValue" class="default">Login</button>

In your style.css:

/* CSS */
.default::after {
  content: "Default Text";
}

.alternate::after {
  content: "Alternate Text";
}

In your index.js:

const createValue = document.getElementById('btnValue');
button.classList.toggle('default');
button.classList.toggle('alternate');

Using Event Listeners

Event listeners are important in web development as they allow us to trigger functions when specific events occur.

In this part, we will explore how to change button text using event listeners.

Click Event Listener

By adding a click event listener to the button, you can constantly change its text whenever it’s clicked:

const createButton = document.getElementById('btnValue');
button.addEventListener('click', () => {
  button.innerHTML = 'Clicked!';
});

Mouseover Event Listener

The mouseover event listener enables you to change the button text when the user hovers over the button:

const createButton = document.getElementById('btnValue');
button.addEventListener('mouseover', () => {
  button.innerHTML = 'Hovering!';
});

Form Submission Event

In form of cases, you can change the button text on form submission to offer feedback to users:

Index.html:

<form>
  <!-- form fields -->
  <button id="submitBtn" type="submit">Submit</button>
</form>

Index.js:

const submitButton = document.getElementById('submitBtn');
submitButton.addEventListener('click', (event) => {
  event.preventDefault();
  submitButton.innerHTML = 'Submitting...';

});

FAQs

How can I revert the button text to its original state after an event?

To revert the button text to its original state, you can store the initial text in a variable and use it when needed.

Is it possible to change the button text based on user input in a text field?

Yes, you can change the button text based on user input in a text field.

Does changing button text affect accessibility?

Yes, it’s important to consider accessibility when changing button text dynamically. Make sure that the new text is descriptive and meaningful to users with assistive technologies.

Conclusion

In conclusion, we have explored different methods to change button text in JavaScript.

Whether you choose direct manipulation, event-driven changes, or using the third-party libraries, JavaScript provides a functionality range of options for dynamic button text modifications.

Additional Resources

Quick step-by-step summary (click to expand)
  1. How to change text on button click in JavaScript. Read the ‘How to change text on button click in JavaScript?’ section for the details and code.
  2. Using Event Listeners. Read the ‘Using Event Listeners’ section for the details and code.
  3. Conclusion. Read the ‘Conclusion’ section for the details and code.
  4. Additional Resources. Read the ‘Additional Resources’ section for the details and code.

Frequently Asked Questions

Is JavaScript still worth learning in 2026?
Yes. JavaScript runs on 98% of websites for the front-end, dominates the back-end via Node.js, powers mobile apps through React Native, builds desktop tools through Electron, and is the scripting layer for most AI tooling (LangChain.js, OpenAI SDK, Vercel AI). Whether you target web, mobile, AI, or full-stack capstones, JavaScript is the broadest single language you can learn.
What is the difference between var, let, and const?
var is function-scoped, hoisted to the top of its scope, and can be redeclared, which leads to bugs in modern code. let is block-scoped (only visible inside the nearest {}) and can be reassigned. const is block-scoped and cannot be reassigned, although object contents can still mutate. Default to const for everything, switch to let only when you actually need to reassign, and avoid var in any code written after 2017.
Which JavaScript version should I target in 2026?
Target ES2020 (ES11) as the safe baseline because every modern browser and Node.js 14+ supports it fully. ES2022 adds useful features like top-level await, private class fields with the # prefix, and the .at() array method. If you are writing for older browsers (IE11 or older Android WebViews), transpile down with Babel or use a build tool like Vite, esbuild, or webpack.
What is the best free editor for JavaScript?
Visual Studio Code is the industry standard, free, with built-in IntelliSense, debugger, terminal, Git, and a huge extension marketplace (ESLint, Prettier, GitHub Copilot, Tailwind). Install the JavaScript and TypeScript Nightly extension for the latest language features. JetBrains WebStorm is more powerful and free for students with a verified .edu email. For quick scratchpad work, the Chrome DevTools Sources panel includes a workspace and breakpoint debugger.
How do I run JavaScript locally vs in the browser?
In the browser: open DevTools with F12 (or right-click then Inspect), go to the Console tab, type or paste your code, press Enter. For HTML pages, add a script tag pointing to your .js file. Locally with Node.js: download Node from nodejs.org (LTS version), then run node script.js in your terminal from the file folder. Use the same Node setup for backend capstones, API integrations, and scripts that do not need a browser.
What can I build with JavaScript for my BSIT capstone?
Common BSIT capstones in JavaScript: full-stack web apps using React or Vue on the front-end with Node.js and Express on the back-end (MongoDB or MySQL for the database), real-time chat or notification systems using Socket.io, single-page dashboards with Chart.js or D3.js, cross-platform mobile apps with React Native, AI-powered chatbots using OpenAI SDK and LangChain.js, and Chrome extensions for productivity tools. Add Tailwind CSS for the UI and Vercel or Netlify for free deployment.

Adones Evangelista


Programmer & Technical Writer at PIES IT Solution

Adones Evangelista is a programmer and writer at PIES IT Solution, author of over 900 tutorials and error-fix guides at itsourcecode.com. Specializes in JavaScript, Django, Laravel, and Python error debugging covering ValueError, TypeError, AttributeError, ModuleNotFoundError, and RuntimeError, plus C/C++ and PHP capstone projects for BSIT students.

Expertise: JavaScript · Python · Django · Laravel · Error Debugging · C/C++
 · View all posts by Adones Evangelista →

Leave a Comment