Working on creating a to-do list with JavaScript. I begin with a few default 'to-dos' that I coded into the html file. The structure of the list is like this
<h4 style="text-align: center;">Reminders To Do:</h4>
<p style="text-align: center;">Check off each task after completing it!</p>
<ul id="reminders" class="list-group">
<div id="task">
<li class="list-group-item" id="taskCheck"><input type="checkbox" name="task">Take out the trash</li>
</div>
<div id="task">
<li class="list-group-item" id="taskCheck"><input type="checkbox" name="task">Email boss about project</li>
</div>
<div id="task">
<li class="list-group-item" id="taskCheck"><input type="checkbox" name="task">Cancel subscriptions</li>
</div>
<div id="task">
<li class="list-group-item" id="taskCheck"><input type="checkbox" name="task">Go to gym</li>
</div>
</ul>
I am able to add new events to this list by creating a form below with an eventListener on 'submit', etc. Everything there works as it should. The issue arises since I have a function in JS that should set the "style: display: "none";"
hence removing the task from the list when completed. This works fine for the default events.
let eventForm = document.querySelector('#taskForm');
let eventName = document.querySelector('input[name="newTask"]');
let eventDate = document.querySelector('input[name="taskDate"');
let reminders = document.querySelector('#reminders');
let msg = document.querySelector('.msg');
eventForm.addEventListener('submit', onSubmit);
function onSubmit(e) {
e.preventDefault();
if(eventName.value === '' || eventDate.value === '') {
msg.innerHTML = 'Please complete both fields.';
setTimeout(() => msg.remove(), 3000);
} else {
let div = document.createElement('div'); //structure is ul -> div -> li -> input
div.setAttribute('id', 'task');
let li = document.createElement('li');
li.setAttribute('id', 'taskCheck');
li.className += 'list-group-item'; //adds the bootstrap class to li
li.innerHTML += '<input type="checkbox" name="task">'; //since input tag is nested within li element
li.appendChild(document.createTextNode(`${eventName.value} | ${eventDate.value}`));
div.appendChild(li); //adds li to a div within the list
reminders.appendChild(div); //adds div into the list
//resets form fields to empty for another task
eventName.value = '';
eventDate.value = '';
}
}
Even though I see the new events added through my form, there is no response when I check/ uncheck the box.
My function for removing checked events is as such: note I changed the functionality so instead of not showing the event, I'm testing with console.log();
let reminderList = document.querySelectorAll('#reminders li');
let tasksCheck = document.querySelectorAll('input[type="checkbox"]');
tasksCheck.forEach((task, i) => { //i holds the index of each task in the list
task.addEventListener('change', () => {
if(task.checked) {
console.log('Task completed!');
} else {
console.log('Task not completed yet.');
}
})
})
I know the post is very long, would really appreciate help with this.