Textbox initial and new values

Good day everyone.

Quite new to Javascript, I have a small problem I cant seem to solve.
The idea is, when I press my “calculate” button, it will do calculations that I have set down in my function. The values are taken from input text box. The initial value is set to for example “4”, when I change the values in my text box, I want my function to keep using those new values which I have in my textbox. When hitting the calculate button, it will always use the new value.

this.calculate.addEventListener(“click”, calculations.bind(this)); //calculate button

function calculations(){
document.getElementById(“inputtextbox”).value = “4”; //input box, initial value 4
if (inputtextbox !== 4) {
var value = Number(document.getElementById(“inputtextbox”).value); //new values to be used
}
//using the “value” variable to do some maths down the line
}

Thank you for your time

If you carefully read through your calculations function, what does it do?

  • it sets the input value to 4
  • then it looks if the input value is !== 4 (how can that ever be true, you’ve just set it to 4)

Yes, the initial value is set to 4.
I guess my logic is trying to be, if my value I type in the inputtextbox is not 4, it will then use this newly typed value in our variable named value

That’s not how it works, it’s not just the initial value. You always set it back to 4 whenever someone clicks the button.

You can set an initial value in your HTML:

<input id="inputtextbox" value="4">

Then in your code, read the new value from the input if someone clicks the button:

var value = Number(document.getElementById("inputtextbox").value);

This by the way doesn’t work:

if (inputtextbox !== 4)

inputtextbox is the HTML element, not the value.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.