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> <html> <head> <title>CHANGE ATTRIBUTE VALUE</title> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script> <script> $(document).ready(function(){ $('#b').click(function(){ $('#dele').attr("class","eleNew"); }) }) </script> </head> <body> <style> .ele{ height: 220px; width: 200px; background-color: green; } .eleNew{ height: 50px; width: 50px; border-radius: 50%; background-color: red; } </style> <div id="dele" class="ele"></div> <button id="b">Change Attribute</button> </body> </html>
- <!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.
- The<html> tag is used to indicate the beginning of HTML document.
- 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.
- Here we imported open source jquery library support file which is needed for coding using jquery in program.
- 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.
- <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
- 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.
- 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.
- 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’.
- In html block we defined div element with id ‘dele’, class ‘ele’ attributes and button ‘Change Attribute’ defined with id ‘b’.
- 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.