Home > Back-end >  Can’t get value of input field in JavaScript
Can’t get value of input field in JavaScript

Time:01-04

For some reason JavaScript does not give me the value option of my input field. It gives me the value of my button however, so something must be wrong with my input.

This is my code HTML: <input type=“number” id=“inputs”/> JS: Const input = document.getElementById(“inputs”).value

CodePudding user response:

Try console.log(e) on your return function.

This is easier to do in React, but my solution to this problem has been to look at the object on the next level up.

Hence why you are looking for e instead of e.target where the latter is your button element.

The solution will probably look something like e.target.value[0][1], as the object returns a nested collection of objects.

CodePudding user response:

if you are trying to get value of input when it's changed you have to add event listener.

<input placeholder="Enter some text" name="name" />
<p id="values"></p>


const input = document.querySelector('input');
const log = document.getElementById('values');

input.addEventListener('input', updateValue);

function updateValue(e) {
  log.textContent = e.target.value;
}

ref from HTMLElement

  • Related