I have this code in a for loop and it's supposed to make all the items do the useItem function with their name as the parameter
if(invType[i] == "usable"){
console.log(invName[i]);
document.getElementById(i).onclick = function(){useItem(invName[i])};
}
and then this is the useItem function
function useItem(object){
switch(object){
case "sword":
console.log("sword");
break;
}
console.log("finished");
}
What is happening is that in the for loop console.log(invName[i]);
is printing "sword" as it should, and it assigns the element the onclick function ok, but it isn't outputting what it should. It always thinks its undefined, so case "sword":
never runs, but it does print "finished" fine. I just cant see what could be going wrong here.