Javascript get data-id attribute value

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.

Answers 2
  • Get data-id attribute value using onclick event on button in javascript

    <button data-id="5" onclick="followUser(this); return false;">Follow</button>
    function followUser(e){
        var id = e.getAttribute('data-id');

    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=""></script>
    <button type="button" data-id="5" id="Button">Follow</button>
        $('#Button').on('click', function(){
        	var value = $(this).attr('data-id');
  • Back to code snippet queries related javascript