JavaScript - onClick to get the ID of the clicked button

前端 未结 16 1233
名媛妹妹
名媛妹妹 2020-11-22 05:44

How do find the id of the button which is being clicked?


16条回答
  •  情深已故
    2020-11-22 06:15

    Sorry its a late answer but its really quick if you do this :-

    $(document).ready(function() {
      $('button').on('click', function() {
         alert (this.id);
      });
    });
    

    This gets the ID of any button clicked.

    If you want to just get value of button clicked in a certain place, just put them in container like

    buttons here

    and change the code to :-

     $(document).ready(function() {
          $('.myButtons button').on('click', function() {
             alert (this.id);
          });
        });
    

    I hope this helps

提交回复
热议问题