I'm making chess in JavaScript and I have a variable that is supposed to be defined when I click on a button and then when I click on another button that ones value is supposed to change to the variable. I've already made sure that the variable does in fact define as it should after the first click but for some reason the variable becomes "undefined" when I click the second time.
var action = 1;
function Find_Piece(clicked_id, Piece)
{
if (action == 1)
{
var Piece = document.getElementById(clicked_id).value;
alert(Piece)
action = 0;
}
else if (action == 0)
{
document.getElementById(clicked_id).value = Piece;
action = 1;
}
}
Every time I click a button it stores the name of the piece in the variable Piece
. This variable becomes "undefined" when the function is ran again.
Why is this, and how do I fix it?
I have tried removing all unnecessary code but to no avail. I expect the piece to be kept in memory and used again when the function is ran a second time