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

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 −

 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 −

Updated on: 2020-10-03T14:57:06+05:30

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements