I have an eventhandler and functions used to add items to a list and remove items from a list. My first function (handler) is works with the btn elements being stored in a local variable to the functions. If I try to do the same with the remove button I get a reference error in the dev tools. If I move the variable that is getting the remove button out of the function and make it global then it works. Why does one function work with the variable for the button set in the function, and the other function doesn't. Here's my code. The variables in question are commented
var handler = function(event){
//this btn variable does work when declared inside handler function
var btn = document.getElementById('btn');
var input = document.getElementById('input').value;
var item = document.createElement('li');
var list = document.getElementById('list');
var x = document.createElement('button');
item.innerHTML = input;
list.appendChild(item);
item.appendChild(x);
};
//This dlt variable doesn't work if declared in function remove
var dlt = document.getElementById('delete');
var remove = function(event){
var list = document.getElementById('list');
var item = document.querySelector('li');
list.removeChild(item);
};
btn.addEventListener('click', handler);
dlt.addEventListener('click', remove);