Whenever a user enters a value in an input field, my function automatically adds a <li>
element to a <ul>
. Additionally, the function assigns the attribute ( onclick="doneToggle"
) to the created <li>
.
function createListElement() {
var li = document.createElement("li");
ul.appendChild(li);
li.appendChild(document.createTextNode(input.value));
li.setAttribute("onclick", "doneToggle()")
li.setAttribute("style", "width: fit-content")
input.value = "";
}
The doneToggle function is designed to add a specific CSS class to the clicked element, giving the text a "line-through" style.
Here is the existing function:
function doneToggle(element) {
element.classList.toggle("done");
}
I wish for this function to toggle the class of the clicked element without the need to assign IDs to the <li>
elements. Your assistance is greatly appreciated.