All TalkersCode Topics

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

Add Attribute jQuery

Last Updated : Mar 11, 2024

Add Attribute jQuery

In this article we will show you the solution of add attribute jQuery, here we needs to use attr() and click() methods. As we know attr() method 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 and the click() method used for generate these process when user clicks on button.

Step By Step Guide On Add Attribute jQuery :-

Here we defined html block with div element for dynamically add ‘class’ attribute on it and button ‘Add Attribute’ for generate this particular 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’ then there we adding class attribute of ‘ele’ value on div element.

<!DOCTYPE html>
        <title>ADDING ATTRIBUTE</title>
        <script src=""></script>
                    height: 220px;
                    width: 200px;
                    background-color: green;
            <button>Add 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 ‘button’ tag name for generate click event when user clicks on ‘Add Attribute’ button.
  8. Within function we adding class attribute of ‘ele’ on div element with the help of attr() method. In a style block we defined some lines of style properties ‘height of div set to ‘220px’, width set to ‘200px’, background color set to ‘green’’ on class value ‘ele’.
  9. So above seen properties are applied on empty div element when user clicks on ‘Add Attribute’ button. In html block we defined div element without any attribute and button ‘Add Attribute’.
  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 attribute on 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 button ‘Add Attribute’ now user needs to click on button then we can see div element with green background box size.

If your wish to add some other attribute or modify those styles just change it accordingly you will get successful result.

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

Author Image About Dikshita

Passionate Electronics and Communication Engineering student with expertise in web development (HTML, CSS, JS,PHP, Bootstrap, React.js) and content writing. Eager problem solver and tech enthusiast, adept at creating engaging web experiences.

Follow Dikshita On Linkedin 🡪