Computer >> Computer tutorials >  >> Programming >> Javascript

How to return the color of the text with JavaScript?


To return and set the text color, use the JavaScript color property.

Example

You can try to run the following code to return the text color with JavaScript −

<!DOCTYPE html>
<html>
   <body>
      <h2>Heading 2</h2>
      <p id="myId" style="color:green;">Demo Text</p>
      <br>
      <button type="button" onclick="display()">Click to get color</button>
      <script>
         function display() {
            alert(document.getElementById("myId").style.color);
         }
      </script>
   </body>
</html>