Ask Your Question
2

How can JavaScript object be used to modify element.style?

asked 2021-04-08 11:00:00 +0000

lakamha gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2022-01-15 12:00:00 +0000

qstack gravatar image

JavaScript object can be used to modify element.style by using the following syntax:

  1. Get the DOM element you want to modify:
var myElement = document.getElementById("my-element");
  1. Access the style object of the element:
var elementStyle = myElement.style;
  1. Set the properties and values you want to modify:
elementStyle.backgroundColor = "red";
elementStyle.fontSize = "20px";

Here is a full example:

var myElement = document.getElementById("my-element");
var elementStyle = myElement.style;

elementStyle.backgroundColor = "red";
elementStyle.fontSize = "20px";

This will set the background color of the element to red and the font size to 20 pixels.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-04-08 11:00:00 +0000

Seen: 8 times

Last updated: Jan 15 '22