Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

223
Views
Updating value of variable based on value of HTML input

I have an HTML input that accepts only numbers and on my script file I have I variable that gets the value of the input. What I want to do is create another variable that is equal to the first variable. When I put a number on the input field the value of the numInput variable changes but not on the
value variable.
EDIT what I am trying to do is, I want to use the value of the value variable on some functions that I will make.

const numInput = document.getElementById("input");
const value = numInput.value;
<header> 
   <input type="number" name="numInput" id="input" class="input" min="0" value="1" required>
</header>

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

The reason value does not change is because it is saving a copy of the numInput.value content during the assignment, not a pointer or reference (something that I don't think you could do on JS). I would suggest using just using .valueAsNumber as suggested by @Sebastian Simon.

about 4 years ago · Juan Pablo Isaza Report

0

JS objects are the only things in JS that are not cloned in assignment to a new variable. This includes arrays.

Therefore, to mimic the behaviour, you will need to update your value upon every change, typically by adding an event listener. I've chosen to use the change event which will only fire upon blur if the value has changed of fields involving the keyboard and selection/alteration of range/date if the value has changed, though you could use the input event if you want it to change on every keypress, too. Have a quick read:

  • https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/change_event
  • https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/input_event

When doing this, the value can't be a constant, either, as it will change, so it must be a variable instead. I've opted for a let declaration for better code isolation, but by all means use var if you need more flexibility.

const numInput = document.getElementById("input");
let value = numInput.value;

numInput.addEventListener('change', () => {
    value = numInput.value;
});
about 4 years ago · Juan Pablo Isaza Report

0

To accomplish this, JavaScript needs to trigger a function when the input value of the HTML element has been changed. To do so, an event listener is needed.

Try changing the input value so you can see the code in action:

document.getElementById("input").addEventListener('input', function(){
    let inputNumber = this.value
    console.log('your variable value is: ' + inputNumber)
})
<header> 
   <input type="number" name="numInput" id="input" class="input" min="0" value="1" required>
</header>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!