Skip to content

Instantly share code, notes, and snippets.

Created August 18, 2017 06:27
Show Gist options
  • Save anonymous/ca53d0648241ced3c8e971f431479c94 to your computer and use it in GitHub Desktop.
Save anonymous/ca53d0648241ced3c8e971f431479c94 to your computer and use it in GitHub Desktop.
ToDoList
<!-- Note: depends on jQuery being included via Pen Settings -->
<input id="new-todo" type="text" onkeypress="checkForEnter(event);" />
<button onclick="addToDo()" >Add</button>
<button onclick="deleteAll()">Delete All</button>
<ul id="todos"></ul>
function addToDo() {
// Get <input> element via "#id" CSS selector
var todoInput = $("#new-todo");
// Get user's text value from <input> element
var todoText = todoInput.val();
// Build a <li> HTML fragment (as a text string)
// that contains the user's text
var todoHTML = "<li class='todo'>" + todoText + "</li>";
// Get To Do list <ul> element via "#id" CSS selector
var todosList = $("#todos");
// Append new HTML (as a child) to <ul> element
todosList.append(todoHTML);
}
function deleteAll() {
$(".todo").remove();
}
function checkForEnter(event) {
if (event.keyCode === 13) {
addToDo();
return true;
}
var ch = event.which;
if (!( (ch > 48) && (ch < 57))) {
event.preventDefault();
return false;
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment