-3

How do I get the value from a form input? All solutions I found on web recommends using getElemebyId().value but it doen't work for my case. File index.html contains:

</body>
    <form id = "myForm" style="font-size:14" class="form1">
        <input id="myNumber" type="number" name="quantity" min="1" max="21">
    </form>
    <script src="myscript.js"></script>
</body>

File myscript.js contains:

var quantityFromUser = document.getElementById("myNumber").value;

But this doesn't work, the answer to this question states at method 1 that one can use:

document.getElementById('textbox_id').value

but this doesn't work either, .value is not even a method for my case, only thing I get is .nodeValue.

Cezar Cobuz
  • 1,077
  • 1
  • 12
  • 34

1 Answers1

1

You're retrieving the value before the input has a value. You should retrieve the value on an event such as input or change.

For example: https://codepen.io/getreworked/pen/gQygzP

James
  • 1,045
  • 8
  • 17