Get element from which onclick function is called

前端 未结 8 879
野趣味
野趣味 2021-01-18 03:59

I have a button with a onclick attribute which is pointing to the function test().


<         


        
8条回答
  •  醉话见心
    2021-01-18 04:51

    I came across an other extremely simple way to do it in Vanilla JS so I post it here for reference:

    function whoami () {
      var caller = event.target;
      alert("I'm " + caller.textContent);
    }
    
    
    

    I'm not sure about the browser support for it but it works at least on Safari, Firefox and Blink based browsers.

提交回复
热议问题