To get and set data attribute values in jQuery; In this tutorial, you will learn how to get and set data attribute value in jquery using attr() method.
jQuery attr() Method
The jQuery attr() method is used to get or set attributes and values of the selected html elements.
Syntax of jQuery attr() Method
For get an attribute’s value use the below syntax
$(selector).attr(attribute);
Using the below syntax you can set an attribute and values.
$(selector).attr(attribute,value);
To set an attribute and value by using a function using this below syntax.
$(selector).attr(attribute,function(index,currentvalue)) ;
To set multiple attributes and values using this below syntax.
$(selector).attr({attribute:value, attribute:value,...})
Parameters of jQuery attr() method
Parameter | Description |
---|---|
Attribute | Using this parameter to specify the name of the attribute. |
Value | Using this parameter to specify the value of the attribute. |
Function (index, currentvalue) | Using this parameter to specify a function that returns an attribute value to set. Index: This is used to receive the index position of the element in the set. Currentvalue: This is used to provide the current attribute value of selected elements. |
Example – how to get data attribute value in jquery
The following example will show you how to get the data-attribute value on click the html elements.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>jQuery Get Data Attribute Values</title> <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script> <script type="text/javascript"> $(document).ready(function() { $(".btn_click").click(function(){ var gen = $(this).attr("data-gender"); alert("You are selected : " + gen); }); }); </script> </head> <body> <h3>Please select your gender :</h3> <label><input type="radio" name="gender" class="btn_click" data-gender="Male"> Male</label> <label><input type="radio" name="gender" class="btn_click" data-gender="Female"> Female</label> </body> </html>
Output
Please select your gender :
In this above example jquery get Attr() method, You can use the jQuery attr() method to get the data-gender attribute of selected HTML element.
Example – how to get data attribute value in jquery
The following example will show you how to Set the data-attribute value on click the html elements.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Set Data Attribute Values</title> <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script> <script> $(document).ready(function(){ $(".btn_click_attr").click(function(){ $("#link").attr("href", "https://www.tutsmake.com/jquery/"); }); }); </script> </head> <body> <h3>Below link changed when you click on button</h3> <p><a href="https://www.tutsmake.com" id="link">Tutsmake.com</a></p> <button type="button" class="btn_click_attr" style="margin-top: 10px;">Click here to Set Data Attribu Values</button> </body> </html>
Output
Below link changed when you click on button
In the above example jquery set Attr() method, You can use the jQuery attr() method to set the href link value attribute of selected HTML element.
If you want to get data attribute value in HTML, you can use jquery .data(‘attribute’) method, .attr(‘attribute full name’) method to get data attribute value by class, id, name, selected option elements in jQuery.
Recommended jQuery Tutorial
- jQuery Remove Attribute and Disabled Attribute From Element
- jQuery | Event MouseUp By Example
- Event jQuery Mouseleave By Example
- jQuery Event Mouseenter Example
- Event jQuery MouseOver & MouseOut By Example
- keyup jquery event example
- Jquery Click Event Method with E.g.
- Event jQuery. Blur By Example
- jQuery form submit event with example
- keydown function jQuery
- List of jQuery Events Handling Methods with examples
- Jquery Selector by .class | name | #id | Elements
- How to Get the Current Page URL in jQuery
- jQuery Ajax Get() Method Example
- get radio button checked value jquery by id, name, class
- jQuery Set & Get innerWidth & innerHeight Of Html Elements
- jQuery Get Data Text, Id, Attribute Value By Example
- Set data attribute value jquery
- select multiple class in jquery
- How to Remove Attribute Of Html Elements In jQuery
- How to Checked Unchecked Checkbox Using jQuery
- jQuery removeClass & addClass On Button Click By E.g.
- To Remove whitespace From String using jQuery
- jQuery Ajax Post Method Example
- jQuery Ajax Get Method Example
- To Load/Render html Page in div Using jQuery Ajax $.load
- jQuery Sibling Methods – To Find Siblings Elements
- jQuery Find Siblings Elements with Class, id