mirror of
https://github.com/microsoft/frontend-bootcamp.git
synced 2026-01-26 14:56:42 +08:00
changed up steps 1,2,3 to have exercises. removed js implementation of todo app
This commit is contained in:
@@ -1,104 +1,214 @@
|
||||
# JavaScript Demo
|
||||
|
||||
Now that we have a UI that looks like a todo app, we need to make it **function** like a todo app. In this example we are going to use raw JavaScript to explicitly modify our application as we interact with it. This will be in stark contrast to the implicit approach we will take when we do this with React in the next exercise.
|
||||
It's entirely possible to create a website with nothing but HTML and CSS, but as soon as you want user interaction other than links and forms, you'll need to reach for JavaScript, the scripting language of the web. Fortunately, JavaScript has grown up quite a bit since it was introduced in the 90s, and now runs just about everything: web applications, mobile applications, native applications, servers, robots and rocket ships.
|
||||
|
||||
> Keep an eye on how often user actions directly modify the HTML on the page. You'll see this number drop to zero when we start using React.
|
||||
|
||||
## What we're starting with
|
||||
|
||||
This demo starts off with a few functions already in place. Let's walk through what's already here.
|
||||
|
||||
- `clearInput()` - This is a generic, reusable function that takes in a `selector` parameter, finds the first matching element, and sets the element's value to an empty string. This direct modification is called a **side effect**.
|
||||
- `getTodoText()` - This is a helper function that returns the value inside of our text field. Notice how some functions return values and how you can save that return value in a variable.
|
||||
- `filter()` - This function takes in a `filterName` string, and a `button` which is a reference to the clicked button.
|
||||
1. Remove the `selected` class from the previously selected element.
|
||||
2. Add `selected` to the clicked button.
|
||||
3. Set `filterName` to the clicked button's `innerText` value.
|
||||
4. Get all of the todos with [`querySelectAll`](https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll), and then loop through them.
|
||||
5. Set the `hidden` property of each todo based on the filter/state combination.
|
||||
|
||||
## Writing `addTodo` Function
|
||||
|
||||
We start writing all functions with the `function` keyword and the name of our function. Functions can take parameters, but in this case we don't need to pass any through, so we follow the function name with an empty `()`. Everything we want this function to do will then be placed in a set of brackets `{}`.
|
||||
|
||||
```js
|
||||
function addTodo() {}
|
||||
```
|
||||
|
||||
### Creating a Todo Clone
|
||||
|
||||
The first thing we need to do in this function is create a `newTodo` which is a clone of an existing Todo.
|
||||
|
||||
```js
|
||||
function addTodo() {
|
||||
const todo = document.querySelector('.todo');
|
||||
const newTodo = todo.cloneNode(true);
|
||||
}
|
||||
```
|
||||
|
||||
Passing true to our `cloneNode` means it is a deep clone, so we get a copy of the todo's children as well.
|
||||
|
||||
> Note that this approach is very fragile, as it requires a todo node to always be present on the page.
|
||||
|
||||
### Updating the newTodos's text
|
||||
|
||||
With this clone created, we need to update the `innerText` of the node with our todo text, which is returned from `getTodoText()`.
|
||||
|
||||
```js
|
||||
function addTodo() {
|
||||
const todo = document.querySelector('.todo');
|
||||
const newTodo = todo.cloneNode(true);
|
||||
|
||||
newTodo.querySelector('.title').innerText = getTodoText();
|
||||
}
|
||||
```
|
||||
|
||||
We can target a child node by calling `querySelector` again and asking for the child with the `.child` class.
|
||||
|
||||
> Note that if we left off the `()` we'd actually be assigning innerText to the 'function' instead of the function return.
|
||||
|
||||
### Placing the newTodo into the list of todos
|
||||
|
||||
Making a clone only stores the clone inside of our variable. If we want to place it back into the DOM, we'll need to insert it manually. For that we can use [insertBefore](https://developer.mozilla.org/en-US/docs/Web/API/Node/insertBefore).
|
||||
|
||||
This function actually needs to target the parent element, which we can get by calling `todo.parentElement` and passing parameters of `(elementToInsert, elementToInsertBefore)`.
|
||||
|
||||
```js
|
||||
function addTodo() {
|
||||
const todo = document.querySelector('.todo');
|
||||
const newTodo = todo.cloneNode(true);
|
||||
newTodo.querySelector('.title').innerText = getTodoText();
|
||||
todo.parentElement.insertBefore(newTodo, todo);
|
||||
}
|
||||
```
|
||||
|
||||
### Cleanup
|
||||
|
||||
Now that our todo has been inserted into the DOM, we can clear the text input. In the exercise, we'll also update the count of the remaining todos in the footer from here.
|
||||
|
||||
```js
|
||||
function addTodo() {
|
||||
...
|
||||
clearInput('.textfield');
|
||||
}
|
||||
```
|
||||
|
||||
> Note how often we have to reach into the DOM to find nodes, manipulate content, insert back into the DOM and manually change the values in inputs. This is the error prone manipulation that React helps us avoid.
|
||||
|
||||
## Triggering functions from click events
|
||||
|
||||
Now that we have a working `addTodo` function, we need a way to trigger it when the user is ready. This can be done in two ways.
|
||||
|
||||
1. We can find the element with `querySelector`, then set its `onclick` to our function
|
||||
|
||||
```js
|
||||
document.querySelector('.addTodo .submit').onclick = addTodo;
|
||||
```
|
||||
|
||||
2. We can add the function directly to our button in our HTML
|
||||
In this demo we are going to cover a few of the core basics of the language that will help us when we start writing our TODO app. At the end of this demo we will be able to display the number of the letter "a"s in our email input. Here's the markup we're working with:
|
||||
|
||||
```html
|
||||
<button onclick="addTodo()" class="submit">Add</button>
|
||||
<div id="contact-form">
|
||||
<label>Email</label><input id="email" type="email" />
|
||||
<input class="submit" value="Submit" type="submit" />
|
||||
</div>
|
||||
```
|
||||
|
||||
Today we'll use #2, as this is the way it will work in React as well.
|
||||
By the end of the demo we'll have covered the following:
|
||||
|
||||
- Variables
|
||||
- Eventing
|
||||
- Functions
|
||||
- Conditionals
|
||||
- Loops
|
||||
- Interacting with the DOM (Document Object Model)
|
||||
|
||||
## Introduction To Variables
|
||||
|
||||
We can create a new variable with the keywords `var`, `let`, `const` and use them within our application. These variables can contain one of the following types of values:
|
||||
|
||||
> Use `const` for variables you never expect to change, and `let` for anything else. `var` is mostly out of fasion.
|
||||
|
||||
- **boolean**: `true`, `false`
|
||||
- **number**: `1`, `3.14`
|
||||
- **string**: `'single quotes'`, `"double quotes"`, or `` `backticks` ``
|
||||
- **array**: `[ 1, 2, 3, 'hello', 'world']`
|
||||
- **object**: `{ foo: 3, bar: 'hello' }`
|
||||
- **function**: `function(foo) { return foo + 1 }`
|
||||
- **null**
|
||||
- **undefined**
|
||||
|
||||
### Variable Examples
|
||||
|
||||
```js
|
||||
let myBoolean = true;
|
||||
let myNumber = 5;
|
||||
let myString = `Using backticks I can reuse other variables ${myNumber}`;
|
||||
let myArray = [1, 'cat', false, myString];
|
||||
let myObject = { key1: 'value1', anotherKey: myArray };
|
||||
let myFunction = function(myNumberParam) {
|
||||
console.log(myNumber + myNumberParam);
|
||||
};
|
||||
```
|
||||
|
||||
> JavaScript is a loosely typed (dynamic) language, so if you initially store a number in a variable (`let myVar = 0`), you can change it to contain a string by simply writing `myVar = 'hello'` without any trouble.
|
||||
|
||||
### Adding Variables
|
||||
|
||||
Let's start off our demo by adding some variables to our [script tag](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script). The other examples on this page will reference these variables.
|
||||
|
||||
```js
|
||||
const match = 'a';
|
||||
let matches = 0;
|
||||
```
|
||||
|
||||
## Functions
|
||||
|
||||
Functions are reusable pieces of functionality. Functions can take inputs (parameters) and return values (or neither). Functions can be called from within your program, from within other functions, or invoked from within the DOM itself.
|
||||
|
||||
In our example we'll create a function called `displayMatches` (camelCase is typical for functions) and we'll invoke this function every time that our submit button is clicked. For now we'll simply have our function call `alert("I'm Clicked")`, which is a function that creates an alert in your browser.
|
||||
|
||||
```js
|
||||
function displayMatches() {
|
||||
alert("I'm Clicked");
|
||||
}
|
||||
```
|
||||
|
||||
## Events
|
||||
|
||||
Functions on their own don't have any affect on the page. When I declare `function displayMatches()` I have only defined the function, I haven't actually triggered it.
|
||||
|
||||
To execute a function we need to attach it to an event. There are a number of possible events: keyboard strokes, mouse clicks, document loading etc...
|
||||
|
||||
### Add Event Listeners
|
||||
|
||||
To attach a function to an event, we use an [`addEventListener`](https://developer.mozilla.org/en-US/docs/Web/API/EventListener) like this:
|
||||
|
||||
```js
|
||||
window.addEventListener('load', function() {
|
||||
console.log('loaded');
|
||||
});
|
||||
|
||||
window.addEventListener('click', function() {
|
||||
console.log('click');
|
||||
});
|
||||
```
|
||||
|
||||
> The `window` is a reference to the entire HTML document
|
||||
|
||||
### Global Event Handlers
|
||||
|
||||
If this feels a little verbose, you're not alone. Many of the [most common event types](https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers) are added as element properties. This way we can set properties like `onload` or `onclick` like this:
|
||||
|
||||
```js
|
||||
window.onload = function() {
|
||||
console.log('loaded!');
|
||||
};
|
||||
window.onclick = function() {
|
||||
console.log('clicked!');
|
||||
};
|
||||
```
|
||||
|
||||
> Note that only a single function can be assigned to `onload`, but many eventListeners can be added to `load`
|
||||
|
||||
In our example we want to trigger a function based on the click of a button. To do this, we first need to get a reference to the button. We can use `querySelector` to get that reference. And then we can set its `onclick` value just like above.
|
||||
|
||||
```js
|
||||
const button = docment.querySelector('.submit');
|
||||
button.onclick = displayMatches();
|
||||
```
|
||||
|
||||
You can also combine these together like this:
|
||||
|
||||
```js
|
||||
docment.querySelector('.submit').onclick = displayMatches();
|
||||
```
|
||||
|
||||
Wire this up and see you function in action!
|
||||
|
||||
## Iteration
|
||||
|
||||
Next we'll update our function to iterate through a string of letters. We loop over each letter using the [`for of`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of) syntax. We'll use real input later, but for now this verifies that our function is working.
|
||||
|
||||
```js
|
||||
function displayMatches() {
|
||||
const text = 'abcda';
|
||||
for (let letter of text) {
|
||||
console.log(letter);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Conditionals
|
||||
|
||||
Next we want to compare each `letter` with our `match` value, and if they are the same, we will increment our `matches` variable. Remember that `letter = match` would set the `letter` variable to the value in `match`, so to do comparisons, we use the equality operator `==` or the strict equality operator `===`.
|
||||
|
||||
```js
|
||||
function displayMatches() {
|
||||
const text = 'abcda';
|
||||
for (let letter of text) {
|
||||
if (letter === match) {
|
||||
matches++;
|
||||
}
|
||||
}
|
||||
console.log(matches);
|
||||
}
|
||||
```
|
||||
|
||||
> In JavaScript, it's safest to use strict `===` for comparisons, because `==` will try to convert the operands to the same type. For example, `"1" == 1` is true whereas `"1" === 1` is false, but the behavior in certain other cases is [not what you'd expect](https://www.youtube.com/watch?v=et8xNAc2ic8). (See [this video](https://www.destroyallsoftware.com/talks/wat) for more strange JavaScript behavior.)
|
||||
|
||||
## Interacting with the DOM
|
||||
|
||||
Now that we have a function performing all of our logic, it's time to connect this to our DOM by using some of the browser's built-in functions.
|
||||
|
||||
First we need to get a reference to the email field in our app's DOM. To do this, I've added an `id` to the input, and we will call one of JavaScript's oldest methods (IE 5.5), `getElementById`, which we find on the browser-provided `document` global variable. This function will return a reference to that input, and we can store it in the `email` variable.
|
||||
|
||||
```js
|
||||
function displayMatches() {
|
||||
const email = document.getElementById('email');
|
||||
console.log(email);
|
||||
...
|
||||
}
|
||||
```
|
||||
|
||||
Since what we're actually after is the value of the input field, we can set our `text` variable to the string assigned to the email input's `value` key. To see this in action, in Chrome you can right click on the console message created by the code above, choose "save as variable" and then type `variableName.value`.
|
||||
|
||||
```js
|
||||
function displayMatches() {
|
||||
const email = document.getElementById('email');
|
||||
const text = email.value;
|
||||
console.log(text);
|
||||
...
|
||||
}
|
||||
```
|
||||
|
||||
### Returning the values to the DOM
|
||||
|
||||
Now that we've read values from the DOM and fed that into our matching logic, we are ready to return the number of matches to our app. To do this we first need to grab a reference to our submit button, and since this button has no `id` we are going to use the more modern (IE8+) `querySelector` to get it. This function takes any valid CSS selector and returns the first match found.
|
||||
|
||||
```js
|
||||
function displayMatches() {
|
||||
...
|
||||
const submit = document.querySelector('.submit');
|
||||
}
|
||||
```
|
||||
|
||||
Now that we have a reference to the submit input, we can set its value contain to the number of matches.
|
||||
|
||||
```js
|
||||
function displayMatches() {
|
||||
...
|
||||
const submit = document.querySelector('.submit');
|
||||
submit.value = matches + ' matches';
|
||||
}
|
||||
```
|
||||
|
||||
We could also have done this in a single line as follows:
|
||||
|
||||
```js
|
||||
function displayMatches() {
|
||||
...
|
||||
document.querySelector('.submit').value = matches + ' matches';
|
||||
}
|
||||
```
|
||||
|
||||
## Next Step
|
||||
|
||||
[Start our Todo App](../../step1-02/demo/)
|
||||
|
||||
@@ -1,66 +1,19 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<link rel="stylesheet" href="./style.css" />
|
||||
<link rel="stylesheet" href="../css-demo/css-demo-final.css" />
|
||||
</head>
|
||||
<body>
|
||||
<header>
|
||||
<h1>todos - step1-03 demo</h1>
|
||||
<div class="addTodo">
|
||||
<input class="textfield" placeholder="add todo" />
|
||||
<button class="submit">Add</button>
|
||||
<div>
|
||||
<div class="tiles">
|
||||
<div>
|
||||
<h2>Contact Us</h2>
|
||||
<div id="contact-form">
|
||||
<label>Email</label><input id="email" type="email" />
|
||||
<input class="submit" value="Submit" type="submit" />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<nav class="filter">
|
||||
<button class="selected">all</button>
|
||||
<button>active</button>
|
||||
<button>completed</button>
|
||||
</nav>
|
||||
</header>
|
||||
|
||||
<ul class="todos">
|
||||
<li class="todo">
|
||||
<label><input type="checkbox" /> <span class="title"> Todo 1 </span></label>
|
||||
</li>
|
||||
<li class="todo">
|
||||
<label><input type="checkbox" /> <span class="title"> Todo 2 </span></label>
|
||||
</li>
|
||||
<li class="todo">
|
||||
<label><input type="checkbox" /> <span class="title"> Todo 3 </span></label>
|
||||
</li>
|
||||
<li class="todo">
|
||||
<label><input type="checkbox" /> <span class="title"> Todo 4 </span></label>
|
||||
</li>
|
||||
</ul>
|
||||
<footer>
|
||||
<span><i class="remaining">4</i> items left</span>
|
||||
<button class="submit">Clear Completed</button>
|
||||
</footer>
|
||||
</div>
|
||||
</body>
|
||||
|
||||
<script type="text/javascript">
|
||||
function clearInput(selector) {
|
||||
document.querySelector(selector).value = '';
|
||||
}
|
||||
|
||||
function getTodoText() {
|
||||
return document.querySelector('.textfield').value;
|
||||
}
|
||||
|
||||
function filter(button) {
|
||||
document.querySelector('.selected').classList.remove('selected');
|
||||
button.classList.add('selected');
|
||||
|
||||
const filterName = button.innerText;
|
||||
for (let todo of document.querySelectorAll('.todo')) {
|
||||
const checked = todo.querySelector('input').checked === true;
|
||||
if (filterName === 'all') {
|
||||
todo.hidden = false;
|
||||
} else if (filterName === 'active') {
|
||||
todo.hidden = checked;
|
||||
} else if (filterName === 'completed') {
|
||||
todo.hidden = !checked;
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
<script></script>
|
||||
</html>
|
||||
|
||||
@@ -1,49 +0,0 @@
|
||||
body {
|
||||
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
||||
width: 400px;
|
||||
margin: 20px auto;
|
||||
}
|
||||
|
||||
h1 {
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.addTodo {
|
||||
display: flex;
|
||||
}
|
||||
|
||||
.textfield {
|
||||
flex-grow: 1;
|
||||
margin-right: 10px;
|
||||
}
|
||||
|
||||
.submit {
|
||||
border: none;
|
||||
padding: 5px 10px;
|
||||
}
|
||||
|
||||
.filter {
|
||||
margin: 10px 0 0;
|
||||
}
|
||||
|
||||
.filter button {
|
||||
background: transparent;
|
||||
border: none;
|
||||
}
|
||||
|
||||
.filter .selected {
|
||||
border-bottom: 2px solid blue;
|
||||
}
|
||||
|
||||
.todos {
|
||||
list-style: none;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
footer {
|
||||
display: flex;
|
||||
}
|
||||
|
||||
footer span {
|
||||
flex-grow: 1;
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
## Exercise
|
||||
|
||||
Open up the [Todo JavaScript Exercise Page](http://localhost:8080/step1-03/exercise/)
|
||||
|
||||
Open the `index.html` file in this exercise folder.
|
||||
|
||||
### Update Navigation
|
||||
|
||||
1. Add an `onclick` attribute to all three buttons in the navigation.
|
||||
2. In each `onclick` call the `filter` function. In our function we need a reference to the clicked button, so pass in the keyword `this` as the only parameter.
|
||||
|
||||
### Complete the `updateRemaining` function
|
||||
|
||||
1. Using `querySelector`, get a reference to the span with the `remaining` class, and store it in a variable .
|
||||
2. Use [`querySelectorAll`](https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll) to get all of the todos.
|
||||
3. Set the `innerText` of the remaining span to the length of those todos.
|
||||
4. Add `updateRemaining()` to the end of the `addTodo` function.
|
||||
|
||||
### Write a `clearCompleted` function
|
||||
|
||||
1. Get a reference to all of the todos with [`querySelectorAll`](https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll).
|
||||
2. Use a `for (let todo of todos)` loop to iterate over each todo.
|
||||
3. Inside the for loop write an `if` statement to test if the `input` inside of the todo has a checked value of true.
|
||||
> Hint: you can use `querySelector` on any HTML element to find matching child elements.
|
||||
4. Call `todo.remove()` for any todo whose input is checked.
|
||||
5. After the loop is done, run `updateRemaining()`.
|
||||
6. Attach `clearCompleted()` function to the `onclick` of the footer button.
|
||||
7. Test it out!
|
||||
|
||||
@@ -1,80 +1,37 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<link rel="stylesheet" href="./style.css" />
|
||||
</head>
|
||||
<style>
|
||||
label,
|
||||
button {
|
||||
display: block;
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
</style>
|
||||
<body>
|
||||
<header>
|
||||
<h1>todos - step1-03 exercise</h1>
|
||||
<div class="addTodo">
|
||||
<input class="textfield" placeholder="add todo" />
|
||||
<button onclick="addTodo()" class="submit">Add</button>
|
||||
</div>
|
||||
<nav class="filter">
|
||||
<button class="selected">all</button>
|
||||
<button>active</button>
|
||||
<button>completed</button>
|
||||
</nav>
|
||||
</header>
|
||||
<label><input type="checkbox" />Ice cream</label>
|
||||
<label><input type="checkbox" />Pizza</label>
|
||||
<label><input type="checkbox" />Tacos</label>
|
||||
<label><input type="checkbox" />Meatloaf</label>
|
||||
<label><input type="checkbox" />Brocolli</label>
|
||||
|
||||
<ul class="todos">
|
||||
<li class="todo">
|
||||
<label><input type="checkbox" /> <span class="title"> Todo 1 </span></label>
|
||||
</li>
|
||||
<li class="todo">
|
||||
<label><input type="checkbox" /> <span class="title"> Todo 2 </span></label>
|
||||
</li>
|
||||
<li class="todo">
|
||||
<label><input type="checkbox" /> <span class="title"> Todo 3 </span></label>
|
||||
</li>
|
||||
<li class="todo">
|
||||
<label><input type="checkbox" /> <span class="title"> Todo 4 </span></label>
|
||||
</li>
|
||||
</ul>
|
||||
<footer>
|
||||
<span><i class="remaining">4</i> items left</span>
|
||||
<button class="submit">Clear Completed</button>
|
||||
</footer>
|
||||
<button>Display Your Favorites</button>
|
||||
|
||||
<div class="favorites"></div>
|
||||
</body>
|
||||
|
||||
<script type="text/javascript">
|
||||
function clearInput(selector) {
|
||||
document.querySelector(selector).value = '';
|
||||
}
|
||||
|
||||
function getTodoText() {
|
||||
return document.querySelector('.textfield').value;
|
||||
}
|
||||
|
||||
function updateRemaining() {
|
||||
}
|
||||
|
||||
function addTodo() {
|
||||
const todo = document.querySelector('.todo');
|
||||
const newTodo = todo.cloneNode(true);
|
||||
newTodo.querySelector('.title').innerText = getTodoText();
|
||||
todo.parentElement.insertBefore(newTodo, todo);
|
||||
|
||||
clearInput('.textfield');
|
||||
}
|
||||
|
||||
// clearCompleted
|
||||
|
||||
function filter(button) {
|
||||
document.querySelector('.selected').classList.remove('selected');
|
||||
button.classList.add('selected');
|
||||
|
||||
const filterName = button.innerText;
|
||||
for (let todo of document.querySelectorAll('.todo')) {
|
||||
const checked = todo.querySelector('input').checked === true;
|
||||
if (filterName === 'all') {
|
||||
todo.hidden = false;
|
||||
} else if (filterName === 'active') {
|
||||
todo.hidden = checked;
|
||||
} else if (filterName === 'completed') {
|
||||
todo.hidden = !checked;
|
||||
<script>
|
||||
function getFavs() {
|
||||
let favList = [];
|
||||
const inputs = document.querySelectorAll('input');
|
||||
for (const input of inputs) {
|
||||
if (input.checked === true) {
|
||||
favList.push(input.parentNode.textContent);
|
||||
}
|
||||
}
|
||||
document.querySelector('.favorites').textContent = favList.join(' ');
|
||||
}
|
||||
|
||||
let button = document.querySelector('button');
|
||||
|
||||
button.addEventListener('click', getFavs);
|
||||
</script>
|
||||
</html>
|
||||
|
||||
@@ -1,49 +0,0 @@
|
||||
body {
|
||||
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
||||
width: 400px;
|
||||
margin: 20px auto;
|
||||
}
|
||||
|
||||
h1 {
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.addTodo {
|
||||
display: flex;
|
||||
}
|
||||
|
||||
.textfield {
|
||||
flex-grow: 1;
|
||||
margin-right: 10px;
|
||||
}
|
||||
|
||||
.submit {
|
||||
border: none;
|
||||
padding: 5px 10px;
|
||||
}
|
||||
|
||||
.filter {
|
||||
margin: 10px 0 0;
|
||||
}
|
||||
|
||||
.filter button {
|
||||
background: transparent;
|
||||
border: none;
|
||||
}
|
||||
|
||||
.filter .selected {
|
||||
border-bottom: 2px solid blue;
|
||||
}
|
||||
|
||||
.todos {
|
||||
list-style: none;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
footer {
|
||||
display: flex;
|
||||
}
|
||||
|
||||
footer span {
|
||||
flex-grow: 1;
|
||||
}
|
||||
@@ -1,92 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<link rel="stylesheet" href="./style.css" />
|
||||
</head>
|
||||
<body>
|
||||
<header>
|
||||
<h1>todos - step1-03 final</h1>
|
||||
<div class="addTodo">
|
||||
<input class="textfield" placeholder="add todo" />
|
||||
<button onclick="addTodo()" class="submit">Add</button>
|
||||
</div>
|
||||
<nav class="filter">
|
||||
<button onclick="filter(this)" class="selected">all</button>
|
||||
<button onclick="filter(this)">active</button>
|
||||
<button onclick="filter(this)">completed</button>
|
||||
</nav>
|
||||
</header>
|
||||
|
||||
<ul class="todos">
|
||||
<li class="todo">
|
||||
<label><input type="checkbox" /> <span class="title"> Todo 1 </span></label>
|
||||
</li>
|
||||
<li class="todo">
|
||||
<label><input type="checkbox" /> <span class="title"> Todo 2 </span></label>
|
||||
</li>
|
||||
<li class="todo">
|
||||
<label><input type="checkbox" /> <span class="title"> Todo 3 </span></label>
|
||||
</li>
|
||||
<li class="todo">
|
||||
<label><input type="checkbox" /> <span class="title"> Todo 4 </span></label>
|
||||
</li>
|
||||
</ul>
|
||||
<footer>
|
||||
<span><i class="remaining">4</i> items left</span>
|
||||
<button onclick="clearCompleted()" class="submit">Clear Completed</button>
|
||||
</footer>
|
||||
</body>
|
||||
|
||||
<script type="text/javascript">
|
||||
function clearInput(selector) {
|
||||
document.querySelector(selector).value = '';
|
||||
}
|
||||
|
||||
function getTodoText() {
|
||||
return document.querySelector('.textfield').value;
|
||||
}
|
||||
|
||||
function updateRemaining() {
|
||||
const remaining = document.querySelector('.remaining');
|
||||
const todos = document.querySelectorAll('.todo').length;
|
||||
remaining.innerText = todos;
|
||||
}
|
||||
|
||||
function addTodo(ev) {
|
||||
const todo = document.querySelector('.todo');
|
||||
const newTodo = todo.cloneNode(true);
|
||||
newTodo.querySelector('.title').innerText = getTodoText();
|
||||
todo.parentElement.insertBefore(newTodo, todo);
|
||||
|
||||
clearInput('.textfield');
|
||||
updateRemaining();
|
||||
}
|
||||
|
||||
function clearCompleted() {
|
||||
const todos = document.querySelectorAll('.todo');
|
||||
for (let todo of todos) {
|
||||
if (todo.querySelector('input').checked === true) {
|
||||
todo.remove();
|
||||
}
|
||||
}
|
||||
updateRemaining();
|
||||
}
|
||||
|
||||
function filter(button) {
|
||||
document.querySelector('.selected').classList.remove('selected');
|
||||
button.classList.add('selected');
|
||||
|
||||
const filterName = button.innerText;
|
||||
for (let todo of document.querySelectorAll('.todo')) {
|
||||
const checked = todo.querySelector('input').checked === true;
|
||||
if (filterName === 'all') {
|
||||
todo.hidden = false;
|
||||
} else if (filterName === 'active') {
|
||||
todo.hidden = checked;
|
||||
} else if (filterName === 'completed') {
|
||||
todo.hidden = !checked;
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
</html>
|
||||
@@ -1,49 +0,0 @@
|
||||
body {
|
||||
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
||||
width: 400px;
|
||||
margin: 20px auto;
|
||||
}
|
||||
|
||||
h1 {
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.addTodo {
|
||||
display: flex;
|
||||
}
|
||||
|
||||
.textfield {
|
||||
flex-grow: 1;
|
||||
margin-right: 10px;
|
||||
}
|
||||
|
||||
.submit {
|
||||
border: none;
|
||||
padding: 5px 10px;
|
||||
}
|
||||
|
||||
.filter {
|
||||
margin: 10px 0 0;
|
||||
}
|
||||
|
||||
.filter button {
|
||||
background: transparent;
|
||||
border: none;
|
||||
}
|
||||
|
||||
.filter .selected {
|
||||
border-bottom: 2px solid blue;
|
||||
}
|
||||
|
||||
.todos {
|
||||
list-style: none;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
footer {
|
||||
display: flex;
|
||||
}
|
||||
|
||||
footer span {
|
||||
flex-grow: 1;
|
||||
}
|
||||
Reference in New Issue
Block a user