Friday, December 27, 2024
Google search engine
HomeLanguagesJavascriptHow to remove style added with .css() function using JavaScript?

How to remove style added with .css() function using JavaScript?

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>
    <html lang="en">
      
    <head>
        <meta charset="UTF-8">
        <title>JavaScript example</title>
    </head>
      
    <body>
        <div>
            <p id="gfg">
              Welcome to neveropen for neveropen
          </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">
        removestyle = () => {
            document.getElementById(
              "gfg").removeAttribute("style");
            
            document.getElementById(
              "setstyle").removeAttribute("disabled");
            
            document.getElementById(
              "removestyle").setAttribute("disabled", "true");
        };
    </script>
      
    </html>

    
    

    Output:
    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 neveropen for neveropen
          </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>

    
    

    Output:
    Before Styling:

    After clicking on Set Style:

    After clicking on Remove Style:

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments