-
-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rework DOM to better separate elements from events (#1074)
Fixes #332 This way, we: * Fully handle accessing and setting DOM element properties in one chunk, then introduce events, rather than muddling the two together. * Identify that there are two common actions to do, and talk through identifying and addressing this. * Pull out the "what is the character limit" to be defined by the HTML attribute, rather than duplicated in the script. * Practice breaking down the "remaining characters" problem into sub-problems. * Generally solve things more incrementally. * Explicitly call out "do a clean-up refactoring" at the end. Also some misc copy edits.
- Loading branch information
1 parent
c5f5a91
commit 962ecdf
Showing
12 changed files
with
259 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
+++ | ||
title = '🧮 Calculating the remaining characters' | ||
|
||
time = 10 | ||
facilitation = false | ||
emoji= '🧩' | ||
hide_from_overview = true | ||
[objectives] | ||
1='Access properties representing HTML attributes' | ||
[build] | ||
render = 'never' | ||
list = 'local' | ||
publishResources = false | ||
|
||
+++ | ||
|
||
We want to implement Step 3: Calculate the number of characters left. | ||
|
||
Let's break down Step 3 into sub-goals: | ||
|
||
```mermaid | ||
flowchart TD | ||
A[Step 3.1: Get the character limit] --> B[Step 3.2: Get the number of characters already typed] --> C[Step 3.3: Subtract already typed from the limit] | ||
``` | ||
|
||
## Getting information from the DOM | ||
|
||
We have seen that the DOM exposes live information about HTML elements in the page via properties on the objects it returns. | ||
|
||
We wrote `textarea.value` to get the characters already typed. This solves Step 3.2 - we can write `textarea.value.length`. | ||
|
||
We can also access the character limit, because it's defined as the `maxlength` attribute of the HTML textarea. | ||
|
||
In the Dev Tools console, if you type `textarea.max` you should see autocomplete for a property called `maxLength`. | ||
|
||
Most HTML attributes are exposed in the DOM as a property with the same name (but in camelCase). Let's try: | ||
|
||
```js | ||
console.log(textarea.maxLength) | ||
``` | ||
|
||
Now that we have the character limit (from `textarea.maxLength`), and the number of characters already typed (from `textarea.value.length`): | ||
|
||
```js | ||
const remainingCharacters = textarea.maxLength - textarea.value.length; | ||
console.log(remainingCharacters); | ||
``` | ||
|
||
Try typing in your textarea, then running this in the Dev Tools console. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.