There are many cases, especially as the content gets more interactive, where the developer want styles to dynamically kick in based on user input, some code having run in the background, and more. In these sorts of scenarios, the CSS model involving style rules or inline styles doesn’t help.
The solution to overcome all of these problems is one that involves JavaScript/jQuery. It not only lets style the element users are interacting with, more importantly, but it also allows developers to style elements all over the page. This freedom is very powerful and goes well beyond CSS’s limited ability to style content inside itself.
- JavaScript:
- removeAttribute():It remove an attribute with specified name from the element.
- setAttribute():It sets the value of an attribute on the specified element.
Example-1: Below example illustrates how to set/remove styles in JavaScript.
<!DOCTYPE html><htmllang="en"><head><metacharset="UTF-8"><title>JavaScript example</title></head><body><div><pid="gfg">Welcome to geeks for geeks</p></div><buttontype="button"id="setstyle"onclick="setstyle()">Set Style</button><buttontype="button"id="removestyle"onclick="removestyle()"disabled>Remove Style</button></body><scripttype="text/javascript">removestyle = () => {document.getElementById("gfg").removeAttribute("style");document.getElementById("setstyle").removeAttribute("disabled");document.getElementById("removestyle").setAttribute("disabled", "true");};</script></html>chevron_rightfilter_noneOutput:
Before Styling:

After clicking on Set Style:

After clicking on Remove Style:
- jQuery:
- css():It sets or returns one or more style properties for selected elements.
- attr():It is used to set or return attributes and values of the selected elements.
- removeAttr():It removes one or more attributes from the selected elements.
Example-2: Below example illustrates how to set/remove styles in jQuery.<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>Jquery example</title>integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60="crossorigin="anonymous"></script></head><body><div><p id="gfg">Welcome to geeksforgeeks</p></div><button type="button"id="setstyle"onclick="setstyle()">Set Style</button><button type="button"id="removestyle"onclick="removestyle()"disabled>Remove Style</button></body><script type="text/javascript">$(document).ready(() => {setstyle = () => {$("#gfg").css({"color":"white","background-color":"green","padding":"10px",});$("#setstyle").attr("disabled","true");$("#removestyle").removeAttr("disabled");}});</script></html>chevron_rightfilter_noneOutput:
Before Styling:

After clicking on Set Style:

After clicking on Remove Style:
Recommended Posts:
- How to remove CSS style of <style> tag using JavaScript/jQuery ?
- How to get a notification when an element is added to the page using JavaScript ?
- How to remove CSS property using JavaScript?
- How to style a checkbox using CSS?
- How to style a dropdown using CSS?
- How to style icon color, size, and shadow by using CSS ?
- How to check an element has certain CSS style using jQuery ?
- How to change the style of alert box using CSS ?
- How to create a style tag using JavaScript?
- How to change style attribute of an element dynamically using JavaScript ?
- How to change font style using drop-down list in JavaScript ?
- Remove border from IFrame using CSS
- How to remove underline for anchors tag using CSS?
- How to remove indentation from an unordered list item using CSS?
- How to remove underline from a:before using CSS ?
- CSS | border-inline-start-style Property
- Types of CSS (Cascading Style Sheet)
- CSS | font-style Property
- CSS | list-style-type Property
- CSS | list-style-position Property
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


