Change Text Color On Hover with JavaScript

In this tutorial we will learn How to Change Text Color On Hover using JavaScript. onmouseover event can be used to create hover effect and JavaScript style property can be used to change text color of element.

onmouseover event

onmouseover event occurs when mouse cursor is moved over a HTML element.

In this example onmouseover event will execute a JavaScript function to change the text color of div tag which will mimic the on hover effect.

style property

style property returns the style properties and values of HTML element.

In this example style property is used to change text color of div.

HTML Code

HTML Code is given below, in this code we have a div tag with onmouseover event and onmouseout event.

onmouseout event is used to revert the changes made.

<!DOCTYPE html>
<html>
<head>
<title>JavaScript Change Text Color On Hover</title>
</head>             
<body>
    
<div onmouseover="mouseOver(this)" onmouseout="mouseOut(this)">
Hover Over me to change my Color!
</div> 
    
<script src="script.js"></script>    
</body>
</html>

JavaScript Code

Take a look at the JavaScript code, In this code user defined function mouseOver() will change text color of div on mouse hover.

element represent the tag which has triggered the onmouseover event.

The mouseOut() will be executed when mouse cursor is moved out of element.

<script>
function mouseOver(element)
{
    element.style.color = 'red';
}

function mouseOut(element)
{
    element.style.color = 'black';
}
</script>

Demo

Video Tutorial

Watch video tutorial on how to Change Text Color On Hover using JavaScript.

Change font-size using JavaScript Get Font Size of any Tag using JavaScript Change Position of HTML Element using JavaScript How to Change Image on Hover with JavaScript How to Disable Button by Class Name in JavaScript How To Change Image Size with JavaScript How to change Image opacity with JavaScript How to Change image src on click with JavaScript How to get the lang attribute value in JavaScript How to Get attribute Value using JavaScript How To Check if Attribute Exists or not with JavaScript How To Count number of links on Page with JavaScript How To Scroll Page to Bottom using JavaScript How To Detect Shift Key Press with JavaScript Change Text Color On Hover with JavaScript Hide and Show div using JavaScript Get Button text with JavaScript Get textarea value with JavaScript Get table row Height with JavaScript Auto Increase width of input field with JavaScript Set Textarea maxLength with JavaScript Set Textarea Value with JavaScript JavaScript Count list items JavaScript set input field value Count Button Clicks with JavaScript Scroll Page to the Top on Click with JavaScript