
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to get value of data attribute and use it in jQuery?
To get value of data attribute, use −
$(“yourSelector”).data()
The following is our input type with data attribute −
<input type="text" id="txtValue" src="" data-value="90">
Example
Following is the code −
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> <script src="https://code.jquery.com/jquery-1.12.4.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> <body> <input type="text" id="txtValue" src="" data-value="90"> </body> <script> var value = $('#txtValue').data('value'); console.log("The value is=" + value); </script> </html>
Output
The output is as follows −
- Related Questions & Answers
- How to use jQuery to get the value of HTML attribute of elements?
- How to use attr() method in jQuery to get the value of an attribute?
- How to get an attribute value in jQuery?
- How to get the value of id attribute in jQuery?
- How to get the value of src attribute in jQuery?
- How to get the value of custom attribute in jQuery?
- jQuery [attribute!=value] Selector
- How to use *= operator in jQuery attribute selector?
- How to use GET method to send data in jQuery Ajax?
- Get value of any attribute from XML data in JavaScript?
- How to find an element based on a data-attribute value in jQuery?
- How to change the value of an attribute using jQuery?
- How to use OR Operation in jQuery Attribute Selectors?
- How to find an element based on a data-attribute value using jQuery
- How do we use # in jQuery Attribute Selector?
Advertisements