Проблема с input.value & addEventListener - PullRequest
0 голосов
/ 02 апреля 2020

Я делаю ввод HTML с помощью простой кнопки. Моя цель - взять значение ввода с идентификатором «esercizio08» и распечатать его, если нажать W.

    <input id="esercizio08" name="test" type="text" value=""></input>
    <input type="button" value="..." id="button08"></input>
________

    let bottone = document.getElementById("button08");

    bottone.addEventListener("Click", controllo);

function controllo(){
            let valore = document.getElementById("esercizio08").value;
            return console.log(valore);
        };

1 Ответ

0 голосов
/ 02 апреля 2020

Ваш код довольно хорошо. Проблема в том, что слушатель имеет имя click, а не Click (маленький 'c').

const bottone = document.getElementById("button08");
bottone.addEventListener("click", controllo);

function controllo(){
   let valore = document.getElementById("esercizio08").value;
   console.log(valore);
};
<input id="esercizio08" name="test" type="text" value=""/>
<input type="button" value="..." id="button08"/>

Только для информации: я также удалил return до console.log(), потому что он не нужен, и я изменил <input></input>, чтобы быть одиночным тег <input/>.

...