Skip to content

Instantly share code, notes, and snippets.

@d-kang
Created August 23, 2018 02:25
Show Gist options
  • Save d-kang/98301f4373a4af3e0399aed1cda837a1 to your computer and use it in GitHub Desktop.
Save d-kang/98301f4373a4af3e0399aed1cda837a1 to your computer and use it in GitHub Desktop.
<html>
<head></head>
<body>
<ul id="todo-app">
<li class="todo"><span>Walk the dog</span></li>
<li class="todo"><span>Pay bills</span></li>
<li class="todo"><span>Make dinner</span></li>
<li class="todo"><span>Code for one hour</span></li>
</ul>
<script>
const addListeners = () => {
const todos = document.querySelectorAll('.todo > span');
for (let i = 0; i < todos.length; i++) {
const todo = todos[i];
if (typeof todo === 'object') {
todo.addEventListener('click', (e) => {
e.target.style = 'text-decoration: line-through';
alert(`Item ${i + 1}: "${e.target.innerText}" is done!`);
});
}
}
};
document.addEventListener("DOMContentLoaded", function (event) {
addListeners();
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment