What is Attribute in JQuery?
All HTML elements have attributes. It provides additional information about an element. Let us see some example of attributes below.
- class Name
- id
- title
- name
- src
- style
Consider the HTML markup for an div element.
1 2 |
<div id='divUserEmailId' class=''txtclass'> </div> |
In the above HTML markup id, class are represents the element’s attributes, which consists of a name and a value. Jquery provide an easy way to manipulate the element attributes by using selectively.
How to get Attribute Value?
The attr() method can be used to either fetch the value of an attribute or perform the assigned operation on the onto all matched elements.
Example :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<h2>Jquery Attribute Example</h2> <html> <head> <title>JQuery-Helpers</title> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"> </script> <script type="text/javascript" language="javascript"> $(document).ready(function () { $(".jQClass").attr("title", "JQuery-Helpers"); }); </script> </head> <body> <div class="jQClass"></div> <div class="jQClass"></div> <div class="jQClass"></div> </body> </html> |
Explanation:
From the above code , title =”JQuery-Helpers” attribute has added with the value for all the div elements which has the class name “jQClass”.