0

Как получить значение поля ввода


У меня подобный вопрос, но предложенные варианты решения не помогают совсем.

function click() {
    let x =  document.getElementsByTagName('input')[0].value;
    console.log(x);
}

    <input type="text" id="inp" onchange='click()'>

2 ответа 2

2

const input = document.getElementById('inp');

// Если хотите, чтоб при каждом изменение значения формы выполнялось действие
input.addEventListener('input', function(event){ console.log(this.value) });
 
// если нужно единоразово получить значение где-то
console.log(input.value);
<input type="text" id="inp" onchange=''>

0

input.oninput = e => console.log(e.target.value);
// или
// input.oninput = function() { console.log(this.value) };
<input type="text" id="input">

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.