JavaScript - onClick to get the ID of the clicked button

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

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


相关标签:
16条回答
  • 2020-11-22 06:08

    In general, things are easier to keep organized if you separate your code and your markup. Define all of your elements, and then in your JavaScript section, define the various actions that should be performed on those elements.

    When an event handler is called, it's called within the context of the element that was clicked on. So, the identifier this will refer to the DOM element that you clicked on. You can then access attributes of the element through that identifier.

    For example:

    <button id="1">Button 1</button>
    <button id="2">Button 2</button>
    <button id="3">Button 3</button>
    
    <script type="text/javascript">
    var reply_click = function()
    {
        alert("Button clicked, id "+this.id+", text"+this.innerHTML);
    }
    document.getElementById('1').onclick = reply_click;
    document.getElementById('2').onclick = reply_click;
    document.getElementById('3').onclick = reply_click;
    </script>
    
    0 讨论(0)
  • 2020-11-22 06:09
    <button id="1" onClick="reply_click(this)"></button>
    <button id="2" onClick="reply_click(this)"></button>
    <button id="3" onClick="reply_click(this)"></button>
    
    function reply_click(obj)
    {
    var id = obj.id;
    }
    
    0 讨论(0)
  • 2020-11-22 06:11

    If you don't want to pass any arguments to the onclick function, just use event.target to get the clicked element:

    <button id="1" onClick="reply_click()"></button>
    <button id="2" onClick="reply_click()"></button>
    <button id="3" onClick="reply_click()"></button>
    
    function reply_click()
    {
        // event.target is the element that is clicked (button in this case).
        console.log(event.target.id);
    }
    
    0 讨论(0)
  • 2020-11-22 06:12

    USING PURE JAVASCRIPT: I know it's late but may be for the future people it can help:

    In the HTML part:

    <button id="1" onClick="reply_click()"></button>
    <button id="2" onClick="reply_click()"></button>
    <button id="3" onClick="reply_click()"></button>
    

    In the Javascipt Controller:

    function reply_click()
    {
        alert(event.srcElement.id);
    }
    

    This way we don't have to bind the 'id' of the Element at the time of calling the javascript function.

    0 讨论(0)
  • 2020-11-22 06:13

    Button 1 Button 2 Button 3

    var reply_click = function() { 
         alert("Button clicked, id "+this.id+", text"+this.innerHTML); 
    } 
    document.getElementById('1').onclick = reply_click; 
    document.getElementById('2').onclick = reply_click; 
    document.getElementById('3').onclick = reply_click;
    
    0 讨论(0)
  • 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

    <div id = "myButtons"> buttons here </div>
    

    and change the code to :-

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

    I hope this helps

    0 讨论(0)
提交回复
热议问题