Jump to content

Button info passed to function


IndianaGuy

Recommended Posts

Can I send a value of a buotton to a JavaScript? or the id or anything at all. II don't care if it gets the value/name/or id. I just need a unique identifier telling the function which button was clicked. Thank you

<button type="button" value="Value_test" id="id_test" name=" Name_Test" onclick="mySwitch(this)">Click Me!</button>
 
function mySwitch(x){
xVal = document.getElementById(x).value; 
console.log("var x =  " + xVal);
{
Edited by IndianaGuy
Link to comment
Share on other sites

In your code the variable "x" already contains a reference to the button. You can get its value, name or innerHTML.

function mySwitch(x){
  xVal = x.value; 
  console.log("var x =  " + xVal);
}
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...