
Javascript get data-id attribute value
Javascript get data-id attribute value
We’ll attempt to use programming in this lesson to solve the "Javascript get data-id attribute value" puzzle.
Sometimes we have to get the value of the custom data-id attribute from the html element and we can get data-id attribute value using javascript using the onclick event on the element.-
Get data-id attribute value using onclick event on button in javascript
<button data-id="5" onclick="followUser(this); return false;">Follow</button> <script> function followUser(e){ var id = e.getAttribute('data-id'); console.log(id); } </script>
This code snippet will help you to get the value of data-id attribute using onclick event on button in javascript.
-
JQuery get data id attribute value on click button
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <button type="button" data-id="5" id="Button">Follow</button> <script> $(document).ready(function(){ $('#Button').on('click', function(){ var value = $(this).attr('data-id'); alert(value); console.log(value); }); }); </script>
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
Don't forget to share this article! Help us spread the word by clicking the share button below.
We appreciate your support and are committed to providing you valuable and informative content.
We are thankful for your never ending support.
Random Code Snippet Queries: Javascript
- How can I add a key/value pair to a JavaScript object
- Execute function with condition in javascript
- How to get file extension using javascript/jquery
- How to Set data to localstorage in javascript
- Hide div on click outside of element in javascript/jQuery
- Get multiplication in javascript using array input
- Check if value exists in array javascript
- How to check file is an image in javascript using filename
- Uncaught TypeError: Cannot read property 'addEventListener' of undefined
- How to fixed bootstrap navbar at top on scroll
- How to pass array as arguments to function in javascript
- How to add key/value pair to existing form data
- How to push string in an array in Javascript
- How to make entire Div clickable in javascript
- Javascript to print Fibonacci series upto 15 Observations
- Uncaught TypeError: $(...).summernote is not a function
- How to crop multiple images with cropper js
- How to set href value of anchor tag in javascript
- Cropper js with multiple images
- ReferenceError: Cannot access 'myFunction' before initialization
- Json stringify
- Input type file styling
- How to preview a word document in HTML using javascript
- How to get data from localstorage in javascript
- Deault order of record in datatable