简体   繁体   中英

Can’t get value of input field in JavaScript

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM