How to get value of data attribute and use it in jQuery?
https://www.tutorialspoint.com/how-to-get-value-of-data-attribute-and-use-it-in-jquery
-
07-10-2020 - |
Question
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 −
Advertisements
Not affiliated with Tutorialspoint