I don't understand why my global var doesn't work inside of the if statement / function.
myBtn = ["btn01", "btn02", "btn03", "btn04"];
var i;
var btnId;
for (i = 0; i < myBtn.length; i++) {
if (document.getElementById(myBtn[i])) {
btnId = myBtn[i];
document.getElementById(btnId).addEventListener("click", function() {
btnValue = document.getElementById(btnId).value;
btnName = document.getElementById(btnId).name;
});
}
};
console.log(btnValue);
/* Then continue on to use the values from btnValue and btnName */
What its meant to do;
Check if Id on button exists in HTML page
If exist then get the following tags (id=, value=, name=)
Then have the 3 values usable outside of the if statement above.
The console.log(btnValue);
displays undefined