javascript - Using multiple buttons on same function that redirects to different functions -
so..i'm having problem couple of days not knowing how this,and need help.
i have multiple buttons , clicking of them redirecting me same function , function going function specified button. idea how can go true couple of functions knowing button clicked? example :
<html> <button type="button" onclick="myfunction()" id="1">button1</button> <button type="button" onclick="myfunction()" id="2">button1</button> <button type="button" onclick="myfunction()" id="3">button1</button> <script> function myfunction() { var x=0; if (button 1){ x=1; myfunction1(x);} if (button 2){ x=2; myfunction2(x);} if (button 3){ x=3; myfunction3(x);} ... myfunction3(x){ alert(x); } } </script> </html>
easiest way pass in element function:
<button type="button" onclick="myfunction(this)" id="1">button1</button> <button type="button" onclick="myfunction(this)" id="2">button1</button> <button type="button" onclick="myfunction(this)" id="3">button1</button> function myfunction(elem) { alert(elem.id); }
no need think event arguments or that.
Comments
Post a Comment