The Wayback Machine - https://web.archive.org/web/20240814120219/https://www.geeksforgeeks.org/how-to-change-style-attribute-of-an-element-dynamically-using-javascript/
Open In App

How to change style attribute of an element dynamically using JavaScript ?

Last Updated : 03 Aug, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Given an HTML document and the task is to change the style properties (CSS Properties) of an element dynamically with the help of JavaScript.

Approach: Using element.style Property

The element.style property is represented by the CSSStyleDeclaration object, which is returned via the style property.

  • Select the element whose style properties need to be changed.
  • Use element.style property to set the style attribute of an element.
  • Set the properties either by using bracket notation or dash notation.

Example 1: This example changes the color and background color of the heading element.

html




<!DOCTYPE html>
<html>
 
<body>
    <h1 id="h1" style="color:green;">
        GeeksforGeeks
    </h1>
 
    <p>
        Click on the button to change
        the style attribute
    </p>
 
    <button onclick="gfg_Run()">
        Click here
    </button>
 
    <p id="result"></p>
 
    <script>
        let res = document.getElementById("result");
        let heading = document.getElementById("h1");
 
        function gfg_Run() {
            heading.style["background-color"] = "green";
            heading.style["color"] = "white";
            res.innerHTML = "Style Attribute Changed";
        }       
    </script>
</body>
 
</html>


Output

How to change style attribute of an element dynamically using JavaScript ?

How to change style attribute of an element dynamically using JavaScript ?

Example 2: This example changes the color, background color, and width properties of elements. 

html




<!DOCTYPE html>
<html>
 
<body>
    <h1 id="h1" style="color:green;">
        GeeksforGeeks
    </h1>
 
    <p id="para">
        Click on the button to change
        the style attribute
    </p>
 
    <button onclick="gfg_Run()">
        Click here
    </button>
 
    <p id="result"></p>
 
    <script>
        let heading = document.getElementById("h1");
        let para = document.getElementById("para");
        let res = document.getElementById("result");
 
        function gfg_Run() {
            heading.style["color"] = "white";
            heading.style["background-color"] = "green";
            heading.style["width"] = "300px";
            heading.style["border"] = "1px solid black";
 
            para.style["background-color"] = "green";
            para.style["width"] = "400px";
            para.style["border"] = "1px solid black";
 
            res.innerHTML = "Style Attribute Changed";
        }       
    </script>
</body>
 
</html>


Output:

How to change style attribute of an element dynamically using JavaScript ?

How to change style attribute of an element dynamically using JavaScript ?



Similar Reads

How to set id attribute of an element dynamically using AngularJS ?
In this article, we will see how to change the ID of any element dynamically using AngularJS, along with understanding its implementation through examples. Approach 1: In this approach, a function is called when the button is clicked, which changes the ID of the element to id-5. We are calling a function on the scope variable and changing the ID fr
3 min read
How to dynamically change the title of web page using JavaScript ?
Given a web page containing the page title, the task is to change the title of a web page dynamically using JavaScript.  The Page title describes the concise description of the web page that is displayed at the top of the browser tab. The &lt;title&gt; tag is used to specify the document's title, The title which should just be text, is displayed in
3 min read
How to Change Image Dynamically when User Scrolls using JavaScript ?
We are going to add the functionality to our web-page so that whenever the user scrolls up or scrolls down on the image, then the image changes. We have used only 3 images but it can easily be expanded for multiple images.We are keeping the images on top of each other, this makes sure only one image is visible at a time. When we scroll, we decremen
4 min read
How to change the position of the element dynamically in ReactJS ?
To change the position of an element dynamically based on certain conditions or user interactions, we can access and modify its styling and link to certain events. When these events trigger that in return will change the position of the element PrerequisitesReact JSNPM and Node.jsReact JS Class ComponentsApproach: To change the position of the elem
2 min read
How to change style/class of an element using JavaScript ?
In this article, we will learn how we can change the style/class of an element. If you want to build a cool website or app then UI plays an important role. We can change, add or remove any CSS property from an HTML element on the occurrence of any event with the help of JavaScript. There are two common approaches that allow us to achieve this task.
4 min read
How to Change the Style of &lt;a&gt; Tag Title Attribute ?
The style of &lt;a&gt; (anchor) tag title attribute is pre-defined for the browser and it may differ from one browser to the other. The webpage cannot display the changes in the tool-tip if based on the title attribute. The option is to make a false-tool-tip using CSS properties according to the wish. For doing this, the data-title attribute must b
2 min read
How to change/update image size dynamically using amp-bind in Google AMP ?
Sometimes you want to add custom interactivity to your AMP pages to make your pages look more user-friendly and user calling. Although AMP's pre-built components are limited, so amp-bind is made to overcome this problem. It helps the developer to add custom interactivity to the pages without using AMP's pre-built components. You can use amp-bind to
2 min read
How to change/update CSS class dynamically using amp-bind in Google AMP ?
Sometimes you want to add custom interactivity to your AMP pages to make your pages look more user-friendly and user calling. Although AMP's pre-built components are limited, so amp-bind is made to overcome this problem. It helps the developer to add custom interactivity to the pages without using AMP's pre-built components. You can use amp-bind to
3 min read
How to change/update text dynamically using amp-bind in Google AMP ?
Sometimes you want to add custom interactivity to your AMP pages to make your pages look more user-friendly and user calling. Although AMP's pre-built components are limited, so amp-bind is made to overcome this problem. It helps the developer to add custom interactivity to the pages without using AMP's pre-built components. You can use amp-bind to
2 min read
How to change/update image dynamically using amp-bind in Google AMP ?
Sometimes you want to add custom interactivity to your AMP pages to make your pages look more user-friendly and user calling. Although AMP's pre-built components are limited, so amp-bind is made to overcome this problem. It helps the developer to add custom interactivity to the pages without using AMP's pre-built components. You can use amp-bind to
3 min read
three90RightbarBannerImg