I need to get the result of a variable after it has been through a loop, I have tried getting its value by making it a global variable but it’s value doesn’t change. Is this possible at all?
Here is my code, I am trying to get the value of the variable t:
var t;
function processSplashbacks() {
var splashbacks = document.worktopForm.splashbacks.value;
for (t = 1; t <= splashbacks; t++){
var splashbackInfo = '<label> Length: </label><input type="text" id="length' + t + '" value="0"/>(cm)<br /><label> Width: </label><input type="text" name="width' + t + '" value="0"/>(cm)<br /><br />';
document.getElementById("splashWidLen").innerHTML += 'Splashback ' + t + ' dimensions<br />' + splashbackInfo;
}
}
function deleteSplashbacks() {
document.getElementById("splashWidLen").innerHTML = "";
}
console.log(t);
Thanks!
Try:
From what I see, you’re only calling
console.log(t)in one place. It only logs it when the page loads, which is before the user clicks on the button. You have to callconsole.log(t);again after the function call.That said, a global variable is probably not the best solution. You would be better off making that function
return t;, and then log the return, like this: