All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

jQuery Toggle Between Two Classes

Last Updated : Mar 11, 2024

jQuery Toggle Between Two Classes

In this article we will show you the solution of jQuery toggle between two classes, here we needs to use addClass() and removeClass() methods. The addClass() method adds one or more class names to the selected elements.

The removeClass() method removes one or more class names from the selected elements so with the help of them we can toggle between two classes using jquery.

Step By Step Guide On jQuery Toggle Between Two Classes :-

Here we defined two paragraph tags with class attribute ‘ele’ for shows toggling two classes.

In script block we defined ready() method which is automatically starts loading code of within this function when this program opened on browser and within that we waiting for click event occurrence which is appended with p tag then there we referring active state paragraph tag then removing active class from that and adding active class to remain paragraph tags.

<!DOCTYPE html>
<html>
    <head>
        <title>Toggle Between Two Classes</title>
        <script src="https://code.jquery.com/jquery-3.5.0.js"></script>
        <script>
            $(document).ready(function(){
                $('p').click(function(){
                    $('p.active').removeClass('active');
                    $(this).addClass("active");
                })
            })
        </script>
        </head>
        <body>
            <style>
                .ele{
                    color: green;
                    font-weight: 500;
                }
                .active{
                    color: red;
                    font-weight: 800;
                    border: 3px solid blue;
                    width: fit-content;
                    padding: 10px;
                }
            </style>
            <p class="ele">This is a paragraph tag</p>
            <p class="ele">This is a paragraph tag</p>
        </body>
</html>
  1. <!DOCTYPE html> tag which is instruct the web browser about what version of HTML file written in and it’s not have any ending tag.
  2. The<html> tag is used to indicate the beginning of HTML document.
  3. As above shown <head> tag is contain information about webpage and external file links are declared here. <title> tag is used for set the webpage title.
  4. Here we imported open source jquery library support file which is needed for coding using jquery in program.
  5. Both <head> and <title> tags having their pair end tag, so we need to close the ending tags respectively. If you’re not closed anyone of ending tag properly that is also affect the webpage result.
  6. <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
  7. In script we defined ready() method within that we appending click() method with p element by using tag name ‘p’ for generate click event when user clicks on it.
  8. Within function we needs to find active state paragraph which is done when we appends active statement with p element then we removing active class from that p element by removeClass() method.
  9. Adding ‘active’ class to remaining p element by using ‘this’ keyword and addClass() method. In style tag we defined active class with two lines of style properties ‘color set to red, font weight set to 800, border set to 3px solid in blue color, width set to content size and padding set to 10px’ and class ‘ele’ defined with block of style properties i.e ‘color sets to green, font weight set to 500’.
  10. In html block we defined two paragraph elements with default style class ‘ele’ so it will shown with this class ‘ele’ style.
  11. Both </body>,</html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.

Conclusion :-

In conclusion now we are able to know how to toggle between two classes using jquery.

Before execution of program we needs to confirm internet connection because then only that jquery file will supports defined jquery codes without error.

When we executes program on browser we can see button two para tag lines ‘This is a paragraph tag’ with ‘ele’ class style now user needs to click on any line then class ‘active’ will toggle on it.

I hope this article on jQuery toggle between two classes helps you and the steps and method mentioned above are easy to follow and implement.