Contact Form

Name

Email *

Message *

Cari Blog Ini

Changing Text Color Of An Element

How to Change Text Color Using JavaScript

Changing Text Color of an Element

To change the text color of a given element first we need to access it inside the JavaScript by using the document.getElementById() method.

Once we have the reference to the element, we can assign a new color to its style.color property.

Example

The following code changes the text color of an element with the id "myElement" to red:

```
 document.getElementById("myElement").style.color = "red"; 
```

Changing the Font Color of an HTML Element

To change the font color of an HTML element using JavaScript, we first need to get a reference to this HTML element and then assign a new color to its style.color property.

Example

The following code changes the font color of an element with the id "myElement" to blue:

```
 document.getElementById("myElement").style.color = "blue"; 
```

Using the fontcolor Attribute

We can also change the text color of an element using the fontcolor attribute.

Example

The following code creates an HTML string and then replaces the document's body with it:

```
 var html = "

Hello World

"; document.body.innerHTML = html;
```


Comments