That did help thanks.
I attempted to improve the code myself (it has been a year since I coded) and managed to find a solution to remove the excess JS code in the HTML event listeners.
I would love to learn how I could modify the JS code so the "monster" array becomes integrated into a Prototype and take "monster" variable out of Global Scope.
Code:
<script type="text/javascript">
var monster = {
arms: 0,
legs: 0,
heads: 0,
bodies: 0,
colors: 0,
hair: 0, };
function appendageChoice(values, type) { // pass either the form option for appendage or a number value
/*** VALIDATE VALID USER OPTIONS BEFORE GO ***/
userchoice = parseInt(document.getElementById("uservalue").value); // user entered a number
var elt = document.getElementById("appendage");
userappendage = elt.options[elt.selectedIndex].text; // that took an hour to achieve
if (userchoice >= 0 && userchoice <=5 ) { // validate whether choice is an integer
if (elt.options[elt.selectedIndex].selected) { // validate whether option is selected
monster[userappendage] += parseInt(userchoice); // add the user value to the global Monster Prototype
alert(userappendage + ' ' + userchoice + ' the Monster Prototype for ' + userappendage + ' now stands totally at ' + monster[userappendage]); //
/*** CHANGE STATE IN HTML ***/
var updatedoc = document.getElementById(userappendage);
updatedoc.innerHTML = parseInt(monster[userappendage]);
}
}
}
</script>
Code:
<form name="appendagechoice" id="appendagechoice" onsubmit="appendageChoice(this);return false;">
<input name="uservalue" id="uservalue" value="enter a number" onfocus="this.value=''"></input>
<select name="appendage" id="appendage">
<option value="arms">arms</option>
<option value="legs">legs</option>
<option value="heads">heads</option>
<option value="bodies">bodies</option>
<option value="colors">colors</option>
<option value="hair">hair</option>
</select>
<input type="submit" value="create"></input>
</form>
<div id="output">
<p>this MONSTER now has</p>
<ul>
<li><span id="arms"></span>Arms</li>
<li><span id="legs"></span>Legs</li>
<li><span id="heads"></span>Heads</li>
<li><span id="bodies"></span>Bodies (one body can have many arms, legs, heads)</li>
<li><span id="colors"></span>Colors</li>
<li><span id="hair"></span>Hair Type</li>
</ul>
</div>
Bookmarks