I made a simple unordered list with checkboxes and want a class with strikethrough text to be applied to the li that the checkbox belongs to. The methods I've tried either only work on the first element or get applied to all of them at once.
The HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>Document</title>
</head>
<body>
<h1>Todo List</h1>
<ul>
<li>
<input type="checkbox">
List Item 1
</li>
<li>
<input type="checkbox">
List Item 2
</li>
<li>
<input type="checkbox">
List Item 3
</li>
</ul>
</body>
</html>
The Javascript (only selects the first one):
document.querySelector("input").addEventListener("click", addStrike);
function addStrike() {
document.querySelector("li").classList.toggle("completed");
}
(selects all at once, when using this code I had the jquery CDN in my html)
$("li").on("click", addStrike);
function addStrike() {
$("li").toggleClass("completed")
}