Я пытаюсь получить значение моего ввода, добавленного в DOM. Не уверен, почему я не получаю значение моего ввода. Я продолжаю получать [объект HTMLInputElement] ... любая помощь была бы оценена. Я пробовал пару методов, но все равно не получаю никаких результатов.Получение значения поля ввода и добавление к DOM
// 'use strict';
var button;
var list = document.getElementById("greatList");
//var item = document.getElementById("addItems").value;
//console.log(item);
document.addEventListener('DOMContentLoaded', init);
function init(){
\t button = document.getElementById('addButton');
\t
\t document.querySelector('#addButton').addEventListener('click', addField);
\t //button.addEventListener('click', getItem)
}
function addField(){
\t var item = document.getElementById("addItems").value;
\t var newField = document.createElement('li');
\t newField.appendChild(document.createTextNode(addItems));
\t newField.classList.add('singleLine');
\t list.appendChild(newField);
\t console.log("working");
}
<div class="wrapper">
\t <div class="list">
\t \t <h2>List: </h2>
\t \t <section class="top">
\t \t \t <input type="text" value="input" id="addItems">
\t \t \t <button type="submit" id="addButton" onclick="addField()">Add</button>
\t \t \t <button>Get Random</button>
\t \t </section>
\t \t <hr>
\t \t <section class="result">
\t \t \t <ul id="greatList">
\t \t \t \t <li class="singleLine">Hello</li>
\t \t \t </ul>
\t \t </section>
\t </div>
</div>
Вот bin
Вы want 'n ewField.appendChild (document.createTextNode (item)); 'Не' addItems', который добавляет ваш входной элемент id. – rgthree
спасибо rgthree, который сработал ... любая идея, почему она добавляется дважды? – Lucky500
Yup. У вас есть атрибут 'onclick', вызывающий' addField() '_and_ и' addEventListener' в вашем методе 'init'. Таким образом, он вызывает 'addField' дважды за каждый клик. Удалите атрибут 'onclick' :) – rgthree