I have an array of objects, each with an age and balance property.
I have a form with a self-select field and a function to loop through and populate the self-select with all of the balances.age values.
<form id="myForm">
<select id="selectAge">
<option>Age</option>
</select>
</form>
var balances = [
{
age: 23,
balance: 10000
},
{
age: 25,
balance: 24000
}
]
function getAge(){
for(var i = 0; i < balances.length; i++) {
var opt = balances[i].age;
var el = document.createElement("option");
el.text = opt;
el.value = opt;
select.add(el);
}
}
I'd like to use the selected age value and insert the corresponding balances of the array into some HTML below.
<h2>You should have $<span id="insertBalance"></span>.</h2>
I'm not getting anywhere with this and may have approached this wrong to begin with. How can I find the correct balance for each selected age and display it in my document?