i have a select with a couple of options and I want to change the content of a div using innerHTML.replace every change in the select using JS only, My problem is that the content changes only at once per refresh and than it does not work anymore until i am not refresh the page.
(I am using FF, tried with Chrome too)..
Thank in advance !!
HTML CODE:
function func() {
let mystudents = [{
Name: "AAA",
Year: 2,
Age: 25,
Exceptional: true
},
{
Name: "BBB",
Year: 2,
Age: 25,
Exceptional: false
},
{
Name: "CCC",
Year: 2,
Age: 27,
Exceptional: false
},
{
Name: "DDD",
Year: 2,
Age: 26,
Exceptional: true
}
];
let template1 = document.getElementById('first-item-template');
let selected = mystudents[document.getElementById('student-list').value];
template1.innerHTML = template1.innerHTML.replace('[Name]', selected.Name)
.replace('[Year]', selected.Year)
.replace('[Age]', selected.Age)
.replace('[Exceptional]', selected.Exceptional);
}
<form>
<label for="student-list">Choose a class:</label>
<select name="student-list" id="student-list" oninput="func();">
<option value="0"> Class1 </option>
<option value="1"> Class2 </option>
<option value="2"> Class3 </option>
<option value="3"> Class4 </option>
</select>
<br><br>
</form>
<div id="first-item-template">
<li>
<span>[Name]</span><br>
<label>Year: </label><span>[Year]</span><br>
<label>Age: </label><span>[Age]</span><br>
<label>Exceptional: </label><span>[Exceptional]</span><br>
</li>
</div>