0

I am recently trying to send multiple values to a javascript function, something like:

<a onclick=myfunction(valA)><img onclick=myfunction(valB)></a>

from what I know, that is not possible, I was wondering though if there is any way to send those values to a third function. This function could be in a "listening" mode and returns the values, but I really dont know where to start.

N.B

I know is possible to do something like onclick=myfunction(valA,valB), but I do need something like:

<a onclick=myfunction(valA)><img onclick=myfunction(valB)></a>

Thanks in advance for your help.

Emiliano

Gnu_nix
  • 25
  • 1
  • 6

2 Answers2

0

That is indeed possible, but utterly senseless because it's not even achieving what you describe as the objective. A function can of course accept multiple arguments:

<a onclick="myfunction(valA, valB)">...</a>

This executed the code once, onclick, and passes both values to the same scope/'unit of work', so to speak.

If I'm misunderstanding you and you do want this function to be executed once per available parameter per click, then you could do that too:

<a onclick="myfunction(valA); myfunction(valB);">...</a>
Grant Thomas
  • 44,454
  • 10
  • 85
  • 129
0

You can pass multiple values to a single function like this:

<a onclick="myfunction(valA, valB, valC)"></a>

If you have different functions for each value, you can simple call each function from this single myfunction(valA, valB, valC). It would look something like this:

<script>
function myfunction(valA, valB, valC)
{
aFunction(valA);
bFunction(valB);
cFunction(valC);
}

function aFunction(valA) { /* do stuff */ }
function bFunction(valB) { /* do stuff */ }
function cFunction(valC) { /* do stuff */ }
</script>

Hope it helps.

Gunnar
  • 2,585
  • 4
  • 21
  • 22