How to Change the Color of HTML Element in JavaScript? Last Updated : 23 May, 2025 Comments Improve Suggest changes Like Article Like Report Changing the text color of an HTML element using JavaScript enhances the user experience by making content more interactive and visually engaging. By accessing and modifying the element's style properties through the DOM, we can dynamically update font colors based on user actions or events. For example, changing a warning message to red when a form is 80% completed can help guide the user effectively.Syntax:element.style.color = "blue";Steps to Change the Color of an HTML ElementSelect the Target Element: Use methods like getElementById, getElementsByClassName, or querySelector to obtain a reference to the HTML element you want to modify.Access the Style Property: Use the style attribute of the selected element to access its CSS properties, providing a gateway to dynamic styling.Modify the Color Attribute: Specifically, target the color attribute within the element's style property to dynamically adjust the text color using JavaScript.Example 1: This example illustrates modifying the color of the HTML Element by implementing the getElementById() Method. index.html <!DOCTYPE html> <html> <head> <title>Change Font Color</title> <style> button { margin-top: 0.5rem; padding: 10px 10px 10px 10px; background: crimson; color: white; border: none; border-radius: 6px; transition: all linear 0.5s; cursor: pointer; } button:hover { transform: translate(0px, -5px); } </style> </head> <body> <div id="gfg"> <h1> Welcome To GeeksForGeeks!! </h1> </div> <button onclick="changeColor()"> Change Color </button> <script> function changeColor() { let gfg = document.getElementById("gfg"); gfg.style.color = "green"; } </script> </body> </html> Output:outputNote: If you want to learn Javascript Concept then check out this tutorial - Javascript TutorialExample 2: This example illustrates the change of color in the HTML Element by selecting the color from the dropdown using JavaScript. index.html <!DOCTYPE html> <html> <head> <title> Change Font Color - Method 2 </title> <style> body { margin: 10rem; } </style> </head> <body> <div id="gfg"> <h1> Welcome To GeeksForGeeks!! </h1> </div> <label for="selectColor"> Select a color: </label> <select id="selectColor" onchange="changeColor()"> <option value="black">Black</option> <option value="crimson">Red</option> <option value="green">Green</option> <option value="#0984e3">Blue</option> <option value="cyan">Cyan</option> <option value="#00b894">Mint Leaf</option> <option value="#e84393">Pink</option> </select> <script> // Provide a function to modify the // "gfg" element's font color. function changeColor() { let gfg = document.getElementById("gfg"); let selectColor = document.getElementById("selectColor"); // From the drop-down menu, obtain // the value of the chosen color. let selectedColor = selectColor.options[selectColor.selectedIndex].value; // Set the font color of the "gfg" // element to the selected color gfg.style.color = selectedColor; } </script> </body> </html> Output: Comment More infoAdvertise with us Next Article How to Change the Color of HTML Element in JavaScript? saurabhkumarsharma05 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads How to get/change the HTML with DOM element in JavaScript ? In order to get/access the HTML for a DOM element in JavaScript, the first step is to identify the element base on its id, name, or its tag name. Then, we can use inner.HTML or outer.HTML to get the HTML. Using the getElementById() method: This method gets/identifies the DOM elements using its ID an 3 min read Change the Border of HTML Element in JavaScript In this article, we will see how to change the Border of HTML element using JavaSCript. To change the border of an element, first, we select the element, and then use HTML DOM border style property to change the border. The CSS border properties are given below: border-style: The border-style In Jav 3 min read How to set the color of an element border with JavaScript ? In this article, we will see how to set the color of an element's border with JavaScript. To set the color of an element's border with JavaScript, we can use the element's style property. Method 1: First, we will create a text element and then apply some CSS styles including the border color of the 2 min read How to change style/class of an element using JavaScript ? In this article, we will learn how we can change the style/class of an element. If you want to build a cool website or app then UI plays an important role. We can change, add or remove any CSS property from an HTML element on the occurrence of any event with the help of JavaScript. There are two com 4 min read How to Change the Color of the Alert Box in JavaScript ? An alert box is a dialogue box that pops up on the screen to provide information to the user. It contains a message and an OK button, which allow users to acknowledge the message and close the box. Alert boxes are commonly used in web development to display important messages, warnings, or notificat 5 min read How to Change the Color of the Alert Box in JavaScript ? Alert boxes, often utilized in JavaScript applications, provide users with important notifications or messages. However, their default appearance may not always blend seamlessly with the overall design of your website or application. JavaScript offers several methods to customize alert boxes as list 3 min read How to Select an Element by ID in JavaScript ? In JavaScript, we can use the "id" attribute to interact with HTML elements. The "id" attribute in HTML assigns a unique identifier to an element. This uniqueness makes it easy for JavaScript to precisely target and manipulate specific elements on a webpage. Selecting elements by ID helps in dynamic 2 min read How to Change Font Color in HTML? We can use <font> tag to change the text color in HTML. This tag was used in older versions of HTML but is deprecated in HTML5. So we can use inline CSS as the best and quickest way to change the font color. 1. Change Font Color using <Font> tagThe <font> tag was used to change tex 2 min read How to Change SVG Icon Color on Click in JavaScript ? SVG stands for Scalable Vector Graphics and is a vector image format for two-dimensional images that may be used to generate animations. The XML format defines the SVG document. You can change the color of an SVG using the below methods: Table of Content Using the setAttribute()Using style.fill prop 2 min read How to Change the Button Color in HTML ? Styling of elements enhances the visual appearance and improves the overall user interface. We can change the button color in HTML using different approaches as listed below. Table of Content Using Inline StylingUsing Internal StylingChange the Button Color Using Inline StylingIn this approach, we a 3 min read Like