How to change text color in JavaScript dynamically?

In this article, we will show you how to change text color with JavaScript dynamically.

Our comprehensive, step-by-step tutorial guides you through the process of using the DOM style property to easily update the color of any text element.

With clear explanations and examples, you will be able to add dynamic color changes to your web pages in no time.

So if you want to know the important details concerning this matter, keep reading!

Let’s start to discover how to change the color of text on your web page using JavaScript.

How to change the font color of a text with JavaScript?

JavaScript provides developers with the ability to dynamically manipulate HTML elements, including the option to change the color of text.

If you want to change the color of some text using JavaScript, you can do it by accessing the specific part you want to change and adjusting its appearance with CSS.

To change the color of text with JavaScript, you can make use of the following predefined JavaScript solutions:

Solution 1: Use the style property of HTML element with document.getElementById method

The basic way of changing text color in JavaScript is to use the style property to directly control how it looks.

By changing the color property, you can make the text change its color dynamically.

Syntax:

object.style.color

Here’s an example:

// Select the element with id "myElement"
var element = document.getElementById("myElement");

// Change the text color to blue
element.style.color = "blue";

In this example, we start by identifying a specific HTML element with the id attribute set to “myElement.”

We do this using the document.getElementById method.

Once we have found the element, we proceed to modify its appearance.

Specifically, we change the color of the text contained within the element to blue. We achieve this by setting the color property of the element’s style attribute to “blue.”

As a result, the text will be displayed in a vibrant blue color.

Here’s the complete code:

<!DOCTYPE html>
<html>
<head>
<style>
.center {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  height: 100vh;
}
</style>
</head>
<body>

<div class="center">
  <h4 id="id">Welcome to Itsourcecode!</h4>
  <button type="button" onclick="changeColor()">Click the button</button>
</div>

<script>
function changeColor() {
  document.getElementById("id").style.color = "blue";
}
</script>

</body>
</html>

Output:

Solution 2: Use CSS

Another way to change the font color of text using JavaScript is to use CSS classes.

Here’s an example:

/* Define a CSS class for bluetext */
.blue-text {
  color: blue;
}

JavaScript code:

var element = document.getElementById('myElement');
element.classList.add('blue-text');

In this example, we begin by creating a CSS class named “blue-text.” This class is responsible for setting the text color of an element to red.

Moving on to the JavaScript code, we use the document.getElementById method to locate the specific HTML element with the id “myElement.”

To change the text color within this element, we simply add the “blue-text” class to it using the classList.add method.

As a result, the text inside the element will be displayed in a vibrant blue color.

Solution 3: Changing text color with inline JavaScript

When using inline JavaScript, you can directly insert JavaScript code into an HTML document.

This allows you to easily change the text color by using the style attribute within the HTML tag.

<p style="color: blue;">This text is blue.</p>

Solution 4: Use querySelector() method

You can also use the document.querySelector method to find the first HTML element that matches a specific CSS selector.

By using the CSS selector “#myElement,” we locate the element with the id “myElement.”

Then, we change the font color of the text inside that element to blue by setting its color property to “blue” using the style property.

The querySelector() method is similar to getElementById(), but it allows us to select elements based on any valid CSS selector, not just the id attribute.

This flexibility is useful when we want to select elements based on other attributes or properties.

Here’s an example:

var element = document.querySelector("#myElement");
element.style.color = "blue";

Here’s the complete code:

<!DOCTYPE html>
<html>
<head>
<style>
.center {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  height: 100vh;
}
</style>
</head>
<body>

<div class="center">
  <h4 id="myElement">Welcome to Itsourcecode!</h4>
  <button type="button" onclick="changeColor()">Click the button</button>
</div>

<script>
function changeColor() {
  var element = document.querySelector("#myElement");
  element.style.color = "blue";
}
</script>

</body>
</html>

Output:

Conclusion

In conclusion, this article provides a comprehensive guide on how to change text color dynamically with JavaScript.

It presents various solutions, including using the style property with getElementById, applying CSS classes, using inline JavaScript, and utilizing the querySelector method.

By following the step-by-step instructions and examples provided, you can easily incorporate dynamic color changes into your web pages.

We are hoping that this article provides you with enough information that helps you understand the JavaScript change text color.

You can also check out the following article:

Thank you for reading itsourcecoders 😊.

Quick step-by-step summary (click to expand)
  1. Understand the goal. Read the article intro to know what change text color looks like and when to use it in your JavaScript code.
  2. Set up your JavaScript file. Create a new .js file or open the browser console. Have Node.js installed if the example runs server-side.
  3. Write the core logic. Follow the code block below. It shows the minimal working syntax to change text color.
  4. Test the result. Run the code in Node or your browser. Confirm the expected output prints or the DOM updates as described.
  5. Handle edge cases. Wrap risky code in try/catch. Check for undefined, null, and empty-array inputs before applying the fix in production.

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.

Caren Bautista


Technical Writer at PIES IT Solution

Responsible for crafting clear, well-structured, and beginner-friendly content across the platform. Handles the writing, proofreading, and editorial review of tutorials, guides, and documentation to ensure every article is accurate, readable, and easy to follow.

Expertise: Technical Writing · Content Creation · Documentation · Editorial Writing · JavaScript · TypeScript · Python · Python Errors · HTTP Errors · MS Excel
 · View all posts by Caren Bautista →

Leave a Comment