Question:

Which JavaScript function is used to change the style of an HTML element dynamically?

Show Hint

Real style changes go through the element's style object, like node.style.color.
Updated On: Jul 2, 2026
  • document.getElementById("element").setStyle("color", "red");
  • document.style.change()
  • document.modifyStyle("element").color("red");
  • document.getElementById("element").style.color = "red";
Show Solution
collegedunia
Verified By Collegedunia

The Correct Option is D

Solution and Explanation

Step 1: To change styling from JavaScript you first grab the element. document.getElementById("element") returns the DOM node whose id is "element".

Step 2: Every DOM node exposes a style object. Its properties map to inline CSS. So node.style.color sets the CSS color property directly on that element.

Step 3: Assigning node.style.color = "red" writes an inline style of color:red. This is the correct syntax.

Step 4: The other options are invented method names. There is no setStyle(), no document.style.change(), and no modifyStyle(). Only option D uses real DOM API.

So the answer is option D.
Was this answer helpful?
0
0