All TalkersCode Topics

Follow TalkersCode On Social Media

Change Attribute Value jQuery

Last Updated : Jul 1, 2023

Change Attribute Value jQuery

In this article we will show you the solution of change attribute value jQuery, here we needs to use attr() and click() methods.

The click() method used for loads some process when user clicks on button element defined html code and as we know attr() method used for sets or returns attributes and values of the selected elements.

When this method is used to return the attribute value, it returns the value of the FIRST matched element.

Step By Step Guide On Change Attribute Value jQuery :-

Here we defined html block with div element with class, id attributes for dynamically change class value to another value and button ‘Change Attribute’ defined with id attribute for generate this process when user clicks on it.

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 button’s id ‘b’ then there we changing class attribute value ‘ele’ to ‘eleNew’ value on div element.

<!DOCTYPE html>
        <title>CHANGE ATTRIBUTE VALUE</title>
        <script src=""></script>
                    height: 220px;
                    width: 200px;
                    background-color: green;
                    height: 50px;
                    width: 50px;
                    border-radius: 50%;
                    background-color: red;
            <div id="dele" class="ele"></div>
            <button id="b">Change Attribute</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() method within that we appending click() method with button by using id ‘b’ value for generate click event when user clicks on ‘Add Attribute’ button.
  8. Within function we changing class attribute value ‘ele’ to ‘eleNew’ on div element with the help of attr() method and id ‘dele’ because attr() method added with id attribute.
  9. In a style block we defined some lines of style properties of class ele ‘height of div set to ‘220px’, width set to ‘200px’, background color set to ‘green’’ and another class ‘eleNew’ have some styles ‘width and height set to 50px, border radius set to 50% and background color set to red’ which style of div we can see after clicks on button ‘Change Attribute’.
  10. In html block we defined div element with id ‘dele’, class ‘ele’ attributes and button ‘Change Attribute’ defined with id ‘b’.
  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 change attribute value on specific html elements 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 box with green background color and button ‘Change Attribute’ now user needs to click on button then we can see div element shape changed to small circle with red background because of new class ‘eleNew’ value.

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

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪