0

Here's a code that I have

    <button data-id="123">
        Restore
    </button>
    function aButtonPressed(id){
        alert(id);
    }
    $(document).ready(function() {
        $('button').on('click', function(){aButtonPressed(data-id);});
    });

I want the function to show in alert box string "123". How should I extract the data-id?

hindmost
  • 7,026
  • 3
  • 25
  • 37
Zbigniew Kisły
  • 632
  • 1
  • 6
  • 12

3 Answers3

1

Change to this

$('button').on('click', function(){
alert($(this).data('id'));
})
Jagdish Idhate
  • 6,997
  • 8
  • 31
  • 49
1

Try this

  $('button').on('click', function(){aButtonPressed($(this).attr('data-id'));});
REDEVI_
  • 684
  • 8
  • 18
0

use dataset property : https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/dataset

var btn = document.querySelector('button');

btn.onclick = function(){
      console.log(btn.dataset.id);
};
Ramanlfc
  • 8,108
  • 1
  • 16
  • 24