Set style by property name in JavaScript

Description

The following code shows how to set style by property name.

Example


<html>
<body>
<div id="div1"
style="background-color: red; height: 50px; width: 50px"
onmouseover="this.style.setProperty('background-color', 'blue', '')"
onmouseout="this.style.setProperty('background-color', 'red', '')"></div>
</body><!--from w w  w  . j a v a 2 s .  c  om-->
</html>

Click to view the demo

The code above generates the following result.

Set style by property name in JavaScript