A Todo List with vanilla Javascript, no Jquery/libraries
All this while i have using jquery for simple DOM traversing and manipulation without any second thoughts but here is a simple example on why it is far more simpler and easy to build a TODO List with Vanilla JS without any jquery. Lets dive right into the code and some explanation along the way.
Here is the link to the github repo: https://github.com/arunkp/Simple-todo. Feel free to fork & build on top of it.
At first, you create an index.html with the link to your style.css and app.js as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <link rel="stylesheet" href="style.css"> <title>Todo List</title> </head> <body> <div class="container"> <input autofocus type="text" id="add" placeholder="Type your Task and press Enter"> <ul id="columns"> </ul> <div class="container"> </div> </div> <script type="text/javascript" src="app.js"></script> </body> </html> |
Next you create the style.css where you will style your todo list accordingly,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
body { font-family: -apple-system, BlinkMacSystemFont, sans-serif; } * { box-sizing: border-box; } body { background: #f5f5f5; } .container { max-width: 768px; padding-top: 20px; margin: auto; } input#add { width: 100%; height: 42px; text-indent: 15px; font-size: 16px; border-radius: 4px; border: 1px solid #e0e0e0; } [draggable] { -moz-user-select: none; -webkit-user-select: none; user-select: none; -webkit-user-drag: element; } ul { padding: 0; margin: 0; position: relative; width: 100%; margin-top: 10px; } ul:before { content: "No items Added"; position: absolute; right: 0; left: 0; font-size: 16px; text-align: center; color: rgba(0, 0, 0, 0.3); font-style: italic; top: 15px; z-index: -1; } ul li { position: relative; z-index: 1; list-style-type: none; background: #fff; width: 100%; display: block; padding: 0; padding: 16px 16px 16px 40px; font-size: 14px; border-left: 1px solid #e0e0e0; border-right: 1px solid #e0e0e0; border-bottom: 1px solid #e0e0e0; border-top: 1px solid #fff; -webkit-touch-callout: none; -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; cursor: move; } ul li label { display: block; } ul li:focus, ul li:hover, ul li:active { background: #fffcfc; } ul li:first-of-type { border-top: 1px solid #e0e0e0; border-top-left-radius: 4px; border-top-right-radius: 4px; } ul li:last-of-type { border-bottom-left-radius: 4px; border-bottom-right-radius: 4px; } ul li .options { font-size: 12px; margin-top: 10px; } ul li .options a { margin-right: 10px; color: #2980b9; text-decoration: none; } ul li.column.dragElem { opacity: #fffcfc; border-radius: 4px; } ul li.column.over { border: 2px dashed #2980b9; } ul li.column .imp { position: absolute; top: 16px; right: 16px; color: #e0e0e0; font-size: 18px; text-decoration: none; } ul li.column .imp:hover { text-decoration: none; } ul li.imp-task .imp { color: #e67e22; } ul li .done-img { display: none; position: absolute; left: 16px; top: 16px; color: #27ae60; text-decoration: none; } ul li.done .done-img { display: block; color: #27ae60; } ul li .date { color: #90949c; } |
Then next is the logic with Javascript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
(function(funcName, baseObj) { funcName = funcName || "docReady"; baseObj = baseObj || window; var readyList = []; var readyFired = false; var readyEventHandlersInstalled = false; function ready() { if (!readyFired) { readyFired = true; for (var i = 0; i < readyList.length; i++) { readyList[i].fn.call(window, readyList[i].ctx); } readyList = []; } } function readyStateChange() { if (document.readyState === "complete") { ready(); } } baseObj[funcName] = function(callback, context) { if (typeof callback !== "function") { throw new TypeError("callback for docReady(fn) must be a function"); } if (readyFired) { setTimeout(function() { callback(context); }, 1); return; } else { readyList.push({ fn: callback, ctx: context }); } if (document.readyState === "complete") { setTimeout(ready, 1); } else if (!readyEventHandlersInstalled) { if (document.addEventListener) { document.addEventListener("DOMContentLoaded", ready, false); window.addEventListener("load", ready, false); } else { document.attachEvent("onreadystatechange", readyStateChange); window.attachEvent("onload", ready); } readyEventHandlersInstalled = true; } } })("docReady", window); docReady(function() { document.getElementById("add").focus(); var dragSrcEl = null; var storeArr = []; var counter = 1; var loadnotes = function(e) { console.log(storeArr); if(storeArr.length>0) { storeArr.map(function(elem){ console.log(JSON.parse(elem)); }); }else { // console.log("no data found"); } } var addTask = function(e) { var today = new Date(); var code = e.which; if (code == 13) e.preventDefault(); var val = e.currentTarget.value; if (code == 13 && val.length > 0) { var count = ++counter; stitchHTML(count,val, today); var elem = document.querySelectorAll('.column'); addClickHandlers(); var store = { value: val, timestamp: today.toLocaleString().split(",")[0] }; saveToStore(store); e.currentTarget.value = ""; } var cols = document.querySelectorAll('#columns .column'); [].forEach.call(cols, addDnDHandlers); } function saveToStore(obj) { obj = JSON.stringify(obj); storeArr.push(obj); window.localStorage.setItem("user",storeArr); a = window.localStorage.getItem("user"); console.log(JSON.stringify(a)); console.log(JSON.parse(a)); } function handleDragStart(e) { dragSrcEl = this; e.dataTransfer.effectAllowed = 'move'; e.dataTransfer.setData('text/html', this.outerHTML); this.classList.add('dragElem'); } function handleDragOver(e) { if (e.preventDefault) { e.preventDefault(); } this.classList.add('over'); if (this.classList.contains('dragElem')) { this.classList.remove('dragElem'); } e.dataTransfer.dropEffect = 'move'; return false; } function handleDragLeave(e) { this.classList.remove('over'); } function handleDrop(e) { if (e.stopPropagation) { e.stopPropagation(); } if (dragSrcEl != this) { this.parentNode.removeChild(dragSrcEl); var dropHTML = e.dataTransfer.getData('text/html'); this.insertAdjacentHTML('beforebegin', dropHTML); var dropElem = this.previousSibling; addDnDHandlers(dropElem); } else { this.classList.remove('dragElm'); } this.classList.remove('over'); addClickHandlers(); return false; } function addDnDHandlers(elem) { elem.addEventListener('dragstart', handleDragStart, false); elem.addEventListener('dragover', handleDragOver, false); elem.addEventListener('dragleave', handleDragLeave, false); elem.addEventListener('drop', handleDrop, false); elem.addEventListener('dragend', handleDragEnd, false); } function handleDragEnd(e) { this.classList.remove('over'); } function makeTaskImp(e) { e.preventDefault(); var li = e.currentTarget.closest('li'); if (!li.classList.contains('imp-task')) { li.classList.add('imp-task'); } else { li.classList.remove('imp-task'); } } function addClickHandlers() { var deleteLink = document.querySelectorAll('.delete'); for (var i = 0; i < deleteLink.length; i++) { deleteLink[i].addEventListener('click', deleteTask); } var impLink = document.querySelectorAll('.imp'); for (var j = 0; j < impLink.length; j++) { impLink[j].addEventListener('click', makeTaskImp); } var doneLink = document.querySelectorAll('.done'); for (var k = 0; k < doneLink.length; k++) { doneLink[k].addEventListener('click', doneTask); } } function doneTask(e) { e.preventDefault(); var li = e.currentTarget.closest('li'); li.classList.add('done'); li.querySelector('.imp').remove(); e.currentTarget.remove(); } function deleteTask(e) { e.preventDefault(); e.currentTarget.closest('li').remove(); } function stitchHTML(count, val, date) { var html = '<li class="column" draggable="true" id="task' + count + '">'; html += '<div class="done-img">✔</div>'; html += '<div class="todo-val">'; html += val; html += '</div>'; html += '<div class="options">'; html += '<a href="#" class="delete">Delete</a>'; html += '<a href="#" class="done">Mark Done</a>'; html += '<span class="date" title="' + date.toLocaleString().split(",")[1] + '">' + date.toLocaleString().split(",")[0] + '</span>' html += '</div>'; html += '<a href="#" class="imp">★</a>'; html += '</li>'; document.getElementById('columns').innerHTML += html; } document.getElementById('add').addEventListener('keyup', addTask); loadnotes(); }); |
Finally, you can visit this URL for live preview: https://arunkp.com/todo/