How to get value of data attribute and use it in jQuery?

jQueryWeb DevelopmentFront End Technology

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 −

 Live Demo

<!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 −

raja
Published on 03-Oct-2020 14:57:06
Advertisements