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>

4
  • 2
    Where is the user inputting a number? How are you calling myFunctionVar()? Commented May 21, 2019 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. Commented May 21, 2019 at 21:06
  • Shortly found a good solution here stackoverflow.com/questions/17433557/… Commented May 21, 2019 at 21:14
  • just call the function myFunctionVar() Commented May 21, 2019 at 21:27

2 Answers 2

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>

Sign up to request clarification or add additional context in comments.

Comments

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)

1 Comment

myNumber = Number(e.target.value)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.