0

I want to set the value input by the user to var numberr, Not the value that is already there eg(value="9").

I might be completely doing the wrong thing I just want to know how to store a value Inputted by the user :/

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


function myFunctionVar() {
  document.getElementById("myNumber").value = numberr;
}
<form action="/action_page.php">
  <input type="number" id="myNumber" value="9">
  <input type="submit">

</form>
Olian04
  • 6,480
  • 2
  • 27
  • 54
Joshua
  • 13
  • 4
  • 2
    Where is the user inputting a number? How are you calling `myFunctionVar()`? – j08691 May 21 '19 at 21:05
  • 2
    When does your function get called? If it's called before the user has entered their value then it will see the old value. Maybe you want to set up an event listener to the 'change' event so that your function is called only when the input value changes. – eddiewould May 21 '19 at 21:06
  • Shortly found a good solution here https://stackoverflow.com/questions/17433557/how-to-save-user-input-into-a-variable-in-html-and-js/54037203 – Joshua May 21 '19 at 21:14
  • just call the function `myFunctionVar()` – Aditya Thakur May 21 '19 at 21:27

2 Answers2

1

You can try parseInt

document.getElementById("btnmyNumber").addEventListener("click", myFunctionVar);
function myFunctionVar() {
  var numberr = parseInt(document.getElementById("myNumber").value, 10);
  alert(numberr);
<form >
<input type="number" id="myNumber" value="9">
<input type="submit" id="btnmyNumber">

</form>
0

Try it:

  let myNumber;
  let input = document.querySelector('#myNumber')
  input.addEventListener('input', function(e){
    myNumber = e.target.value
    console.log(myNumber)
  })

Vote up if it helped)