I am creating an ingredient list where it should be possible to change the number of servings and automatically change the number you need of each ingredient. I've managed to do this somewhat, my only problem now is that it ONLY works if I change the number here: value="4" and not in the input-box that shows on the website (which it is supposed to!).
This is the HTML:
<input type="number" id="serving" oninput="changeServing()" value="4"/>
<ul>
<p class="ingredient-numbers"></p>
<li><span class="endre">600</span>g ingredient</li>
<li><span class="endre">400</span> g ingredient</li>
<li><span class="endre">2</span>ingredient</li>
<li><span class="endre">1</span>ingredient</li>
<li><span class="endre">2</span>ingredient</li>
</ul>
JavaScript:
let saveServing = document.getElementById("serving").value;
let saveArray = document.getElementsByClassName("classname-for-all-list-elements-here");
function changeServing() {
for (let i = 0; i < saveArray.length; i++) {
saveArray[i].innerHTML = saveArray[i].innerHTML/4 * saveServing;
}
}
If I change value in HTML (example: change value="4" to value="5" etc.) it works, the problem is that if I change the number in the input box that shows on the website; it does not work. How can I make it work?
Btw, I'm very new to JavaScript so my knowledge is very limited.
PS: The default value should be 4, and change only if it gets input.

saveServingwhen page loads only, before user can provide their input. Get the current value inside the function