I have several blocks of code. The first block adds the users input to the form, the second block tries to grab that data to evaluate a switch statement. Bc javascript reads all scripts once, my var in block two always returns a value of null; the element had no value at the time the .js ran. How do I get the value of an input element created after javascript has read all scripts? Here's a sum of the important parts:
The HTML:
<div class="form-group">
<label for="">Weight Classification:</label>
<input type="text" class="form-control" id="weight-class" name="weight">
</div>
The javascript:
<script>
//prior code grabs height and weight and calcs BMI
switch (BMI >= 16 && BMI < 43) {
case (BMI >= 16 && BMI < 22):
document.getElementById("weight-class").setAttribute("value", "skinny");
break;
case (BMI >= 22 && BMI < 25):
document.getElementById("weight-class").setAttribute("value", "average");
break;
//other cases here...
default:
console.log("Error");
}
</script>
Javascript block 2:
<script>
var weight = document.getElementById('weight-class').value;
switch (weight) {
case (skinny):
console.log("Successful, short");
break;
case (average):
console.log("Successful, average");
break;
case (obese):
console.log("Successful, tall");
break;
default:
console.log("Failed all cases");
break;
}
</script>
<form>in JS, we wouldaddEventListenerand respond to user interactions such as'focus'or'blur'and/or"submit"to do something with the values...😕addEventListener...or you could do a 'weird' callback in asetTimeout.