All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

jQuery Addclass

Last Updated : Mar 11, 2024

jQuery Addclass

In this article we will show you the solution of jQuery addclass, here we are going to show you example for adding class 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.

Step By Step Guide On jQuery Addclass :-

Here we defined html block with heading tag, two para tags with some contents and at last button ‘Add Class’.

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 ‘intro’ class name which is contain two properties of style we are adding that styles on element p and ‘:last’ refers last element of p in html code.

<!DOCTYPE html>
        <title>ADD CLASS USING JQUERY</title>
        <script src=""></script>
                font-size: 120%;
                color: blue;
        <h1>Welcome All</h1>
        <p>This is first paragraph tag</p>
        <p>This is second paragraph tag</p>
        <button>Add Class</button>
  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 class ‘intro’ on p para of last 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 properties ‘font-size as 120%, font color as blue’ for class ‘intro’.
  9. In html block we defined h1 tag with text ‘Welcome All’ and two para tags with respective contents ‘This is first paragraph tag, This is second paragraph tag’ and last button ‘Add Class’.
  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 without error.

When we executes program on browser we can see heading, contents and button user needs to clicks on button then second tag will changed to become big size font with blue color texts based on added class style ‘intro’ to elements.

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

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪