0

I am making a list and when I enter the item into the input box and press enter the first time, the information goes away. If I click the submit button and then press enter I am able to enter the information multiple times that way. How do I submit the information with enter the first time?

var userInput = document.querySelector('.userInput');
var toDoItems = document.querySelector('.toDoItems');
var toDoList = document.querySelector('.toDoList');
var addBtn = document.querySelector('.addBtn');
var resetBtn = document.querySelector('.resetBtn');
var userInput = [];
var newListItems = '';
function clearText() {
    toDoItems.value = '';
}
function render() {
    toDoList.innerHTML = '';
    newListItems = '';
    for (var i = 0; i < userInput.length; i++) {
        newListItems += userInput[i] + '<br>';
        toDoList.innerHTML = newListItems;
    }
}
toDoItems.addEventListener('click', clearText);
addBtn.addEventListener('click', function() {
    userInput.push(toDoItems.value);
    render();
    console.log(toDoItems.value);
});
resetBtn.addEventListener('click', function() {
    userInput = [];
    newListItems = '';
    toDoList.innerHTML = '';
});
  • Your code looks strange. 'userInput' seems to be a control but you are using it as an array. – Jaime Mar 18 '16 at 00:54
  • Regarding your specific question, it has been responded several times in SO. See: http://stackoverflow.com/questions/155188/trigger-a-button-click-with-javascript-on-the-enter-key-in-a-text-box – Jaime Mar 18 '16 at 00:56

0 Answers0