javascripthtmldomgetelementbyidnodevalue

Javascript: Can only get nodeValue() not value() from an input?


I have the following HTML Input:

<input type="number" id="input-number"/>

In my JS code below, when I try to do .value on userInput I cannot do so, I can only do .nodeValue.

Why is this?

// elements on page
const userInput = document.getElementById('input-number');
const addBtn = document.getElementById('btn-add');
const subtractBtn = document.getElementById('btn-subtract');

Solution

  • I just found it's working fine here. You can check this example.

    const userInput = document.getElementById('input-number');
    const addBtn = document.getElementById('btn-add');
    const subtractBtn = document.getElementById('btn-subtract');
    
    addBtn.addEventListener('click', (e) => {
      userInput.value++;
    })
    
    subtractBtn.addEventListener('click', (e) => {
      userInput.value--;
    })
    <input type="number" value=0 id="input-number"/>
    <button id="btn-add">+</button>
    <button id="btn-subtract">-</button>