How to Remove and add Style with .css() Function using JavaScript? Last Updated : 07 Oct, 2024 Summarize Comments Improve Suggest changes Share 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 A AnkitMishra16 Follow Improve Article Tags : JavaScript JavaScript-Questions Similar Reads Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co 11 min read JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav 11 min read Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance 10 min read JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q 15+ min read Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact 12 min read Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and 9 min read 3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power 13 min read What is Vacuum Circuit Breaker? A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac 13 min read Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca 7 min read CTE in SQL In SQL, a Common Table Expression (CTE) is an essential tool for simplifying complex queries and making them more readable. By defining temporary result sets that can be referenced multiple times, a CTE in SQL allows developers to break down complicated logic into manageable parts. CTEs help with hi 6 min read Like