I'm trying to take the two functions at the bottom of my code: //Add item to To-Do List with "Add" Button and "//Add item to list with ENTER KEY" and add the bulk of these functions to the //Add new item to To-Do List function, so the other functions were simpler and the code for the bottom two functions doesn't repeat.
Any advice would really be appreciated!
//Add new item to To-Do List
function addNewItem(list, itemText) {
totalItems++;
var listItem = document.createElement("li");
list.appendChild(listItem);
}
//Add item to list with ENTER KEY
var totalItems = 0;
var inItemText = document.getElementById("inItemText");
inItemText.focus();
inItemText.onkeyup = function(event) {
if (event.which === 13) {
var itemText = inItemText.value;
if (!itemText || itemText === "") {
return false;
}
addNewItem(document.getElementById("todoList"), itemText);
inItemText.focus();
inItemText.select();
}
}
//Add item to To-Do List with "Add" Button
var btnNew = document.getElementById("btnAdd");
btnNew.onclick = function() {
var itemText = inItemText.value;
if (!itemText || itemText === "") {
return false;
}
addNewItem(document.getElementById("todoList"), itemText);
inItemText.focus();
inItemText.select();
}