All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

JavaScript Change Font Color Based On Value

Last Updated : Mar 11, 2024

JavaScript Change Font Color Based On Value

In this article we will show you the solution of JavaScript change font color based on value, we will change the font colors in any HTML page using JavaScript. Also, we will change the font-size using JavaScript.

For doing so, we are going to use the JavaScript eventListner tag. Let us know about the tag first.

addEventListner: A web page responds to an event. Events can be created by users or by APIs. An event listener is a JavaScript process that watches for the occurrence of an event.

Syntax for addEventListner:

addEventListener(type, listener)

in the example below, we will change the font color and the font size by giving input at the <input> tag in HTML page.

Step By Step Guide On JavaScript Change Font Color Based On Value :-

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title> JavaScript change font color based on value </title>
    <style>
        body{
            background-color: aliceblue;
            font-family: Cambria, Cochin, Georgia, Times, 'Times New Roman', serif;
        }
        h1{
          color : rgb(95, 194, 95) ;
          font-size: 25px;
          font-weight : bolder ;
        }
        h3{
            color : rgb(95, 194, 95) ;
            font-size: 20px;
        }
    </style>
</head>
<body>
    <center>
        <h1> TALKERSCODE </h1>
        <h3> JavaScript change font color based on value </h3>
    </center>
    <br>
    <div>
        font color change on value
    </div>
    <br>
    <input type="text" />
    <script>
        let text = document.querySelector("div") ,
        input = document.querySelector("input") ;
        input.addEventListener("input", () => {
            text.style.color = input.value;
            text.style.fontSize = input.value;
        })
    </script>
</body>
</html>
  1. To write HTML code <! DOCTYPE html> to mention which version of the HTML file is written in.
  2. Then we have to write the <html> tag used to define the root of an HTML document. And also write the ending tag </html>.
  3. <head> tag used here to use to contain the metadata about the HTML file and end it with </head> tag
  4. Then <title> tag is used to set the title of the HTML document and end it with </title> tag.
  5. In order to style the HTML page, we'll utilize an external CSS file
  6. using <style> tag to add CSS
  7. <h1> tag used to add a heading close it with </h1> tag
  8. Create a <div> and write some text under it
  9. Now create a <input> tag with the type of text
  10. To write JavaScript, create a <script> tag
  11. Create a variable using let and select the HTML <div> tag using document.querySelector()
  12. Also select the input tag using document.querySelector().
  13. Use .addEventListner tag first.
  14. .style property to give font color and font size by using value property.
  15. Now by providing font color or the font size as input, the respected properties will change of <div> of the HTML file

Conclusion :-

At last, here in conclusion, we can say that with this article’s help, we know How to change font color based on value using JavaScript.

I hope this article on JavaScript change font color based on value helps you and the steps and method mentioned above are easy to follow and implement

Author Image About Amruta

Amruta is an Experienced web developer with 4 years for experience she completed her master's with MCA and passionate about programming Languages for creating technical contents like HTML, CSS, JavaScript, Java, Python, PHP, jQuery.

Follow Amruta On Linkedin 🡪