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,125
  • 3
  • 27
  • 39
Zbigniew Kisły
  • 692
  • 1
  • 6
  • 12

3 Answers3

1

Change to this

$('button').on('click', function(){
alert($(this).data('id'));
})
Jagdish Idhate
  • 7,513
  • 9
  • 35
  • 51
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,283
  • 1
  • 18
  • 24