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.
<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 :
<h2>Jquery Attribute Example</h2> <html> <head> <title>JQuery-Helpers</title> <script type="text/javascript" src="https://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”.