How to Change the Style Attribute of an Element Dynamically.
The description of the parameters is as follows:
- propertyName: specifies the name of property such as color, font-size, etc.
- propertyValue: represents the value to assign to the property of an element, e,g, “red”.
An example is adapted to change the text color by accessing the style attribute of an element. The code is given below by considering the dynamic change of style attributes.
<h2>An example to change the dynamic properties</h2>
<form action="" name="orderForm">
<input type="BUTTON" value="Press Button" onClick="pressBtn()" />
var myElement = document.getElementById("introtext");
The description of the code is explained in a logical order.
- A button is created by passing the value “Press Button”.
- A “pressBtn()” method is attached to this button that is triggered when it is called.
- After that, the above method “pressBtn()” is written within <script> tags.
- In this method, myElement variable is utilized to save the element attributes.
- Finally, the style attributes of this element are dynamically changed when the pressBtn() method is called.
After pressing the button “Press Button”, the pressBtn() method is triggered, which changes the style attribute of this specific element dynamically.