Skip to content
Advertisement

The value in my number input won’t change with user interaction [closed]

I am trying to multiply the value that the user will choose from the input box by an already set value of 0.1 but the value that is fetched from the input is either 0 or stays as the default value already set. The value (ex: 8) that is chosen by the user won’t be taken as the new updated value.

This is the input:

<div>Number:<input type="number" id="NumberToMultiply" min="1" max="100" value="1"></input></div>

This is the script that is fetching the value inputted by the user:

var amount = document.getElementById("NumberToMultiply").value;

This is the line that is making the equation:

const Result = (amount)*(0.1)

The thing is that even if I chose the number “43” with the input, the value fetched from the input is still 1.

Advertisement

Answer

var amount = document.getElementById("NumberToMultiply").value;
const Result = amount * 0.1;
console.log(Result);
document.getElementById("NumberToMultiply").oninput = function () {
    console.log(document.getElementById("NumberToMultiply").value * 0.1);
}

<div>Number:<input type="number" id="NumberToMultiply" min="1" max="100" value="1"></input></div>

That should do the trick, the problem was you didn’t have any event on user trigger, just on load

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement