I have code that works but was wondering if there is a way to condense it with JS loops? I tried but I'm have trouble with it. I'm trying to build a calculator webpage for a game I plan on playing. The "hide" is a material item for the skinning activity in the game. I am anticipating that there will be several material items. Thanks!
total = 0;
hide1 = 1;
hide2 = 1;
hide3 = 1;
function addUp(num, x) {
if (x == "hide1" && hide1 == 1) {
temp = document.theForm.ttl.value;
tempo = parseInt(temp);
numo = parseInt(num);
nwTemp = tempo + numo;
document.theForm.ttl.value = nwTemp;
hide1 = 0;
return hide1;
}
if (x == "hide1" && hide1 == 0) {
temp = document.theForm.ttl.value;
tempo = parseInt(temp);
numo = parseInt(num);
nwTemp = tempo - numo;
document.theForm.ttl.value = nwTemp;
hide1 = 1;
}
if (x == "hide2" && hide2 == 1) {
temp = document.theForm.ttl.value;
tempo = parseInt(temp);
numo = parseInt(num);
nwTemp = tempo + numo;
document.theForm.ttl.value = nwTemp;
hide2 = 0;
return hide2;
}
if (x == "hide2" && hide2 == 0) {
temp = document.theForm.ttl.value;
tempo = parseInt(temp);
numo = parseInt(num);
nwTemp = tempo - numo;
document.theForm.ttl.value = nwTemp;
hide2 = 1;
}
if (x == "hide3" && hide3 == 1) {
temp = document.theForm.ttl.value;
tempo = parseInt(temp);
numo = parseInt(num);
nwTemp = tempo + numo;
document.theForm.ttl.value = nwTemp;
hide3 = 0;
return hide3;
}
if (x == "hide3" && hide3 == 0) {
temp = document.theForm.ttl.value;
tempo = parseInt(temp);
numo = parseInt(num);
nwTemp = tempo - numo;
document.theForm.ttl.value = nwTemp;
hide3 = 1;
}
}
<form name="theForm" style="display:block;border:1px">
<div id="t1" style="display:none;">
<input type="checkbox" onclick="addUp(4, 'hide1')">
Beginner's Leather Hood
<br/>
<input type="checkbox" onClick="addUp(8, 'hide2')">
Beginner's Leather Armor
<br/>
<input type="checkbox" onClick="addUp(4, 'hide3')">
Beginner's Leather Shoes
<br/>
</div>
<div id="t2" style="display:none;">
<input type="checkbox" onclick="addUp(8, 'hide4')">
Novice's Leather Hood
<br/>
<input type="checkbox" onClick="addUp(16, 'hide5')">
Novice's Leather Armor
<br/>
<input type="checkbox" onClick="addUp(8, 'hide6')">
Novice's Leather Shoes
<br/>
</div>
Total Number:
<input type="text" name="ttl" value=0>
</form>