Unable to get value of input box

Hi everyone!

I am trying to get the value of an input box for further computation…
I have on my html:

<div class="form-amount">
    <form action="">
      <label for="input">Enter amount</label>
      <input type="number" id="amount">
    </form>
  </div>

and in my javascript:

let amount = Number(document.getElementById('amount').value);
console.log(amount)

But the value I am getting in the console.log is 0(zero) when I insert a number on my input box.
Any ideas what I am doing wrong?

Hi,

Depending on how you time it
let amount = Number(document.getElementById('amount').value);
gets the value basically towards the beginning of your page loading.

If it gets triggered by another condition, it will only get a value if the value attribute of amount is set.

Look into form handling with js. If it’s getting a value as a form is submitted it’s fairly straight-forward. If it’s something like auto-complete/suggest it’s slightly more involved.