How to Remove and add Style with .css() Function using JavaScript? Last Updated : 07 Oct, 2024 Comments Improve Suggest changes Like Article Like Report There are many cases, especially as the content gets more interactive, where the developer wants 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 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. These are the following approaches:Table of ContentUsing JavaScriptUsing jQueryUsing JavaScriptIn the JavaScript approach, we directly manipulate DOM elements using methods like setAttribute, removeAttribute, and getElementById. The setstyle function uses setAttribute to apply inline CSS styles to the paragraph (<p>) element, changing its text color, background, and padding. It also disables the "Set Style" button and enables the "Remove Style" button by toggling their disabled attributes.The removestyle function removes the inline styles using removeAttribute, and resets the buttons' states accordingly. Example: This example shows the addition of styling using JavaScript. HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>JavaScript example</title> </head> <body> <div> <p id="gfg"> Welcome to Geeks for Geeks </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"> // Define the css() function to set styles function css(element, styles) { for (let property in styles) { element.style[property] = styles[property]; } } function setstyle() { const gfgElement = document.getElementById("gfg"); // Use the css() function to apply styles css(gfgElement, { color: "green", fontSize: "20px" }); document.getElementById("setstyle") .setAttribute("disabled", "true"); document.getElementById("removestyle") .removeAttribute("disabled"); } function removestyle() { const gfgElement = document.getElementById("gfg"); // Reset styles by using the css() function css(gfgElement, { color: "", fontSize: "" }); document.getElementById("setstyle") .removeAttribute("disabled"); document.getElementById("removestyle") .setAttribute("disabled", "true"); } </script> </html> Output:OutputUsing jQueryIn the jQuery approach, the same functionality is achieved using jQuery's simplified DOM manipulation methods like css(), attr(), and removeAttr(). The setstyle function uses css() to directly apply multiple CSS properties to the paragraph element and attr() and removeAttr() to control the buttons' disabled state. The removestyle function removes all inline styles with removeAttr('style') and restores the buttons' states.Example: This example shows the addition of styling using jQuery. HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Jquery example</title> <script src= "https://code.jquery.com/jquery-3.3.1.js" integrity= "sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60=" crossorigin="anonymous"> </script> </head> <body> <div> <p id="gfg"> Welcome to Geeks for Geeks </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"); }; removestyle = () => { $("#gfg").removeAttr("style"); $("#setstyle").removeAttr("disabled"); $("#removestyle").attr("disabled", true); }; }); </script> </html> Output: Comment More infoAdvertise with us Next Article How to Remove and add Style with .css() Function using JavaScript? A AnkitMishra16 Follow Improve Article Tags : JavaScript Web Technologies JQuery JavaScript-Questions Similar Reads 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 com 4 min read How to remove a Class name from an Element using JavaScript ? Removing a class name from an HTML element using JavaScript is often used to dynamically modify the styling or behavior of an element based on user interactions, events, or other conditions. The operation can be achieved using classList property, that allows you to add, remove, or toggle classes. Sy 3 min read How to fade the removal of a DOM Element using CSS and JavaScript ? In this article, we will fade the removal of a DOM Element using CSS and JavaScript.CSS has a vast domain, while basic CSS is essentially copy-pasted, it can become highly complicated when merged with front-end technologies like JavaScript and related frameworks. Therefore it is noticed that some hi 2 min read How to add CSS Rules to the Stylesheet using JavaScript ? In this example, we will see how to add CSS rules to the stylesheet element using JavaScript. First, we will create an HTML document and then add some CSS rules using JavaScript. Syntax: let styleText = document.getElementById('GFG').sheet; let st = `.box { width: 100px; height: 100px; }`; styleText 2 min read How to Remove all Inline Styles using JavaScript/jQuery? To remove all inline styles from an element using JavaScript or jQuery, the removeAttribute method in JavaScript or the removeAttr method in jQuery can be used. Approach: The jQuery attr() and removeAttr() methods are used to remove the inline style property. The attr() method sets the attribute val 2 min read How to remove an HTML element using JavaScript ? Removing an HTML element using JavaScript involves deleting it from the DOM, typically using methods like element.remove() or parentNode.removeChild(). This approach updates the webpage dynamically, allowing for responsive interactions by removing unwanted or outdated elements based on user actions 3 min read How to read CSS rule values with JavaScript? DOM (Document Object Model) object can read and manipulate CSS rules. We can use the following approaches to read all the Embedded CSS rules using JavaScript. Using getElementsByTagName() MethodUsing window.getComputedStyle() MethodApproach 1: Using the getElementsByTagName() MethodUse document.getE 3 min read How to remove specific div element by using JavaScript? Removing a specific `<div>` element with JavaScript is crucial for dynamic web content management. Whether updating information, responding to user actions, or optimizing performance by cleaning the DOM, efficient DOM manipulation is essential. JavaScript offers diverse methods such as `remove 3 min read How to remove CSS style of <style> tag using JavaScript/jQuery ? Given an HTML document containing inline and internal CSS and the task is to remove the style of <style> tag. The internal or embedded CSS is used within the head section of the HTML document. It is enclosed within <style> tag. Approach: The jQuery remove() and empty() methods are used t 2 min read How to remove CSS property using JavaScript? To remove CSS property using JavaScript, we have different methods. In this article, we will learn how to remove CSS property using JavaScript. Below are the Methods used to remove CSS property using JavaScript: Table of Content Using CSS removePropertyUsing the setProperty methodMethod 1: Using CSS 2 min read Like