Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
171
rated 0 times [  178] [ 7]  / answers: 1 / hits: 71839  / 11 Years ago, fri, august 23, 2013, 12:00:00

So..i'm having this problem for couple of days not knowing how to do this,and i need help.



I have multiple buttons and clicking all of them is redirecting me to same function and from that function is going to another function specified for that button.
Any idea how can i go true couple of functions knowing which button is 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>

More From » html

 Answers
55

Easiest way would probably be to pass in the element into the 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 to think about event arguments or anything like that.


[#76169] Friday, August 23, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
havenbilliec

Total Points: 324
Total Questions: 106
Total Answers: 94

Location: Pitcairn Islands
Member since Fri, Oct 15, 2021
3 Years ago
;