A Simple To-Do List App

I build this simple app with less than 150 lines of code. jQuery really makes it simple to use Javascript when you’re a total noob myself.

I wouldn’t recommend you skip out in learning vanilla javascript though. Understanding the concepts of Javascript makes it simpler to understand and implement jQuery.


TO-DO LIST




  • Build a todo list app

  • Publish on my teachMeJavascript site

  • Tell everyone about it


HTML

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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Todo List App</title>
<script src="assets/js/lib/jquery-3.2.1.min.js" charset="utf-8"></script>
<link href="https://fonts.googleapis.com/css?family=Roboto:400,500,700" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css">
<link rel="stylesheet" href="assets/css/todos.css">
</head>
<body>
<div id="container">
<h1>TO-DO LIST <i class="fa fa-plus" aria-hidden="true"></i>
</h1>
<input type="text" placeholder="Add New Todo">
<ul class="todo">
<li><span><i class="fa fa-trash-o" aria-hidden="true"></i></span> Build a todo list app</li>
<li><span><i class="fa fa-trash-o" aria-hidden="true"></i></span> Publish on my teachMeJavascript site</li>
<li><span><i class="fa fa-trash-o" aria-hidden="true"></i></span> Tell everyone about it</li>
</ul>
</div>
<script src="assets/js/todos.js" charset="utf-8"></script>
</body>
</html>

CSS

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
body {
font-family: Roboto;
background: #43cea2; /* fallback for old browsers */
background: -webkit-linear-gradient(to right, #185a9d, #43cea2); /* Chrome 10-25, Safari 5.1-6 */
background: linear-gradient(to right, #185a9d, #43cea2); /* W3C, IE 10+/ Edge, Firefox 16+, Chrome 26+, Opera 12+, Safari 7+ */
}
h1 {
background: #2980b9;
color: white;
margin: 0 auto;
padding: 10px 20px;
font-size: 24px;
font-weight: 400;
text-transform: uppercase;
}
ul {
list-style: none;
margin: 0;
padding: 0;
}
li {
background: #fff;
height: 40px;
line-height: 40px;
color: #666;
}
li:hover span {
width: 40px;
opacity: 1.0;
}
li:nth-child(2n) {
background: #f7f7f7;
}
span {
background-color: #e74c3c;
height: 40px;
margin-right: 20px;
text-align: center;
color: white;
width: 0px;
display: inline-block;
transition: 0.2s linear;
opacity: 0;
}
input {
font-size: 18px;
color: #2980b9;
background-color: #f7f7f7;
width: 100%;
padding: 13px 13px 13px 20px;
box-sizing: border-box;
border: 3px solid rgba(0, 0, 0, 0);
}
input:focus{
background: #fff;
border: 3px solid #2980b9;
outline: none;
}
.fa-plus {
float: right;
}
.completed {
color: gray;
text-decoration: line-through;
}
#container {
width: 360px;
margin: 100px auto;
background: #f7f7f7;
box-shadow: 0 0 3px rgba(0, 0, 0, 0.5);
}

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
//Check off specific todos by clicking
$("ul").on("click", "li", function(){
$(this).toggleClass("completed");
});
//click on x to delete todos
$("ul").on("click", "span", function(event){
$(this).parent().fadeOut(500, function(){
$(this).remove();
});
event.stopPropagation();
});
//input
$("input[type='text']").keypress(function(event){
if(event.which === 13){
//grab todo text from input
var todoText = $(this).val();
$(this).val("");
//create a new li and add to ul
$("ul").append("<li>" + "<span><i class='fa fa-trash-o' aria-hidden='true'></i></span> " + todoText + "</li>")
}
});
//click listener plus sign
$(".fa-plus").click(function(){
$("input[type='text']").fadeToggle();
});