• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / JavaScript Change Text Color of a Paragraph

JavaScript Change Text Color of a Paragraph

January 29, 2022 Leave a Comment

To change the text color of a paragraph using JavaScript, we simply use the Style color property.

document.getElementById("div1").style.color = "green";

Let’s say we have the following HTML:

<div id="div1">
  <p id="p1">We will change the color of this text.</p>
</div>

If we want to change the color of the paragraph to green, we will use the color property in the following JavaScript code.

document.getElementById("p1").style.color = "green";

The second argument can be any valid CSS color. So for example, we could use the hexadecimal value for green to change the color to green.

document.getElementById("p1").style.color = "#00FF00";

Using JavaScript to Change the Color of Text with a Click

To change the color of text using JavaScript, we can target the style color property with a click event.

Let’s say we have the following HTML and we want to change the color of the first paragraph to green.

<div>
  <p id="p1">This is some text</p>
  <p id="p2">This is some text</p>
  <div id="click-me" onclick="clickFunction()">Change text color</div>
</div>

We can utilize both the style color property of #p1 along with an onclick event to change the color of the first line of text.

Here is the JavaScript code:

function clickFunction() {
     document.getElementById("p1").style.color = "green";
}

The final code and output for this example of how to change the text color using JavaScript is below:

Code Output:

This is some text

This is some text

Change text color

Full Code:

<div>
  <p id="p1">This is some text</p>
  <p id="p2">This is some text</p>
  <div id="click-me" onclick="clickFunction()">Change text color</div>
</div>

<script>

function clickFunction() {
     document.getElementById("p1").style.color = "green";
}

</script>

Hopefully this article has been useful for you to understand how to use JavaScript to change the color of some text.

Other Articles You'll Also Like:

  • 1.  JavaScript indexOf – Get the position of a value within a string
  • 2.  Using JavaScript to Return String
  • 3.  How to Call a JavaScript Function From HTML
  • 4.  cleartimeout JavaScript – How the clearTimeout() Method in JavaScript Works
  • 5.  Using JavaScript to Get Date Format in yyyy-mm-dd hh mm ss
  • 6.  Remove the First Element From Array in JavaScript
  • 7.  JavaScript cos – Find Cosine of Number in Radians Using Math.cos()
  • 8.  Using JavaScript to Calculate Average of Array of Numbers
  • 9.  JavaScript Random Color – How to Generate a Random Color In JavaScript
  • 10.  Using JavaScript to Reverse a Number

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy