How to useJavaScript in HTML

Define a JavaScript function called changeColor that is used to change the color of an HTML element with the id “myElement”. Here we used <script> tagto add javascript code in HTML.

Syntax

<script>
<!-- javascript body -->
</script>

Example: Illustration of adding color in HTML without CSS Using JavaScript

HTML




<!DOCTYPE html>
<html>
 
<head>
    <title>JavaScript Example</title>
    <script>
        function changeColor() {
            var element = document.getElementById("myElement");
            element.style.color = "green";
        }
    </script>
</head>
 
<body onload="changeColor()">
    <h1 id="myElement">Using javascript to change color of this text</h1>
</body>
 
</html>


Output:

Output

How to add color in HTML without CSS ?

In HTML, adding color to text and elements is typically done using CSS. However, there are several approaches to add color directly in HTML, without using CSS.

We will discuss the following approaches to add color without CSS in HTML Elements.

Table of Content

  • Using Font Tag
  • Using JavaScript
  • Using SVG tag with fill attribute
  • Using Text Color Attribute
  • Using the bgcolor Attribute

Similar Reads

Approach 1: Using Font Tag

Use tag to specify the color of text within the tag. In font tags, we have to use color attributes to provide color names or codes....

Approach 2: Using JavaScript

...

Approach 3: Using SVG tag with fill attribute

Define a JavaScript function called changeColor that is used to change the color of an HTML element with the id “myElement”. Here we used