All TalkersCode Topics

Follow TalkersCode On Social Media

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

jQuery Add Multiple Classes

Last Updated : Mar 11, 2024

jQuery Add Multiple Classes

In this article we will show you the solution of jQuery add multiple classes and how to add multiple classes on particular element using jquery.

Here we are going to show you example for adding multiple classes in jquery with the help of addClass() method.

The addClass() method adds one or more class names to the selected elements.

This method does not remove existing class attributes, it only adds one or more class names to the class attribute and when we adding more classes need to give space between class names.

Step By Step Guide On jQuery Add Multiple Classes :-

Here we defined html block with heading tag, para p tag with some content and lastly button ‘Add 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 defined click() method with element name ‘button’ so which is refers when clicks on button click() event will load particular process.

There we defined addClass() method with ‘highlight, w’ two class names which are contains two sets of property of styles and we added that styles on element p in html code.

<!DOCTYPE html>
<html>
    <head>
        <title>ADD MULTIPLE CLASS USING JQUERY</title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
        <script>
            $(document).ready(function(){
                $("button").click(function(){
           $("p").addClass("highlight w");
                });
            });
        </script>
    </head>
    <body>
        <style>
            .highlight{
                background-color: yellow;
                width: fit-content;
            }
            .w{
                font-weight: bold;
                font-size: 150%;
            }
        </style>
        <h1>Welcome All</h1>
        <p>This is first paragraph tag</p>
        <button>Add Classes</button>
    </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() and click() method within that we adding two classes ‘highlight, w’ on p para element in html code by using ‘addClass()’ method.
  8. So which process will begin when user clicks on button after loading this program on browser and in style block we defined two sets of property. The highlight class has ‘background color of content is yellow, width to fit content size’ and class ‘w’ has ‘font-weight as bold, font-size as 150%’ styles.
  9. In html block we defined h1 tag with text ‘Welcome All’ and para tag with content ‘This is first paragraph tag’ and last button ‘Add Classes’.
  10. 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 add class in jquery. Before execution of program we needs to confirm internet connection because then only that jquery file will supports defined jquery codes in document without any error.

When we executes this program on browser we can see heading ‘Welcome All’, content ‘This is first paragraph tag’ and button ‘Add Classes’ user needs to clicks on button then para tag content will changed to become big size font with yellow background color like highlighted text based on added classes styles.

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

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪