- [Instructor] All right, let's start with the interactive components of our app. There are two interactive elements here, the test area, where we need to detect input, and here we need to do two things, first, detect the very first keystroke to start the clock, then, match the string of text that is being entered into this box with the string of text above. So we need to count the number of letters, and then count the number of letters in the string, and make sure that the two match. The second interactive element is the Start Over button, I just need to detect a click, here, and for all of this, I need a couple of event listeners.
So at the very bottom of my script, I'm going to start by setting up an eventListener for this box. But remember, this box is the test area box, so I'll say, testArea, addEventListener. Here I'm detecting keyboard events, and there are a ton of keyboard events to detect. In this case, I want to start the timer when the first key is pressed, so I'm going to use the keypress event, that will trigger the start function, which starts the timer.
Then I can set up that start function. And inside the start function, I need to make sure that I'm only detecting the very first keypress, so I'll do a quick little cheat here, and detect how much content currently sits inside this box, and if that content that currently sits there is equal to zero, then we start the timer. So I'll set up a let, with the name textEnteredLength, and set that equal to testArea.
Here we want the value of the testArea, so that's what's being entered, and a length property. If I console.log textEnteredLength, and open the console, you'll see that the first keystroke I make has the number zero. That's because at the time of that keystroke, there were zero letters inside this box, because the keypress event happens before the content actually gets entered into that value.
That also means, for the spellchecking component, where I need to match the key just entered in with the text up here in the box, I need to detect a different event. I'll go back to my script and add testArea, again. This time, I want to detect a keyup event, so that's when you let go of the key, and when the keyup event happens, I'll run a new function called spellCheck, and I'll set up that new function.
And here, we'll set up a new let, with the name textEntered, and set that equal to testArea and value. If I console.log out this value, you'll see in the console here, that when I input content, I first capture the number of strokes already in the box, so the first key will be zero, then, when I let go of the key, we output the actual text entered.
So if I put in A here, we get zero, then A. If I put in B, we get one, AB. C, two, ABC. That means I have the structural components in place to both start the timer, and do a proper string there between the string inside the test area, and the test string. That leaves one interactive component, the Start Over button. This is just a standard button, so we're going to grab the button, resetButton, addEventListener.
Here we're detecting click, and when the click happens, we run the reset function. Reset function is right here, and for now, we'll just console.log out reset button has been pressed! Save that, click the button. Every time we click it, reset button has been pressed. All right, that means we have all the interactive components in place, and we're ready for the next step.
Author
Updated
4/1/2019Released
5/17/2017Through practical examples and mini-projects, this course helps you build your understanding of JavaScript piece by piece, from core principles like variables, data types, conditionals, and functions through advanced topics including loops, closures, and DOM scripting. Along the way, you will also be introduced to some ES6 and the basics of JavaScript libraries.
- What is JavaScript?
- Working with data
- Using functions and objects
- Working with JavaScript and the DOM
- Changing DOM elements
- Handling events
- Working with loops
- Making images responsive using markup
- Troubleshooting code
- Validating functionality
- Minifying JavaScript
Skill Level Beginner
Duration
Views
Related Courses
-
Introduction
-
Welcome1m 7s
-
-
1. JavaScript: An Introduction
-
What is JavaScript?2m 38s
-
-
2. The Basics
-
Introducing the browser console10m 31s
-
3. Working with data
-
Data types in JavaScript4m 2s
-
Arrays2m 20s
-
4. Functions and Objects
-
Functions in JavaScript3m 28s
-
Build a basic function3m 29s
-
Anonymous functions5m 11s
-
Variable scope3m 17s
-
ES2015: let and const6m 12s
-
Make sense of objects3m 19s
-
Object constructors6m 16s
-
Closures8m 11s
-
-
5. JavaScript and the DOM, Part 1: Changing DOM Elements
-
Access and change elements4m 33s
-
Access and change classes3m 45s
-
Access and change attributes4m 53s
-
Add DOM elements6m 56s
-
6. Project: Create an Analog Clock
-
Use CSS to move clock hands3m 49s
-
7. JavaScript and the DOM, Part 2: Events
-
What are DOM events?1m 31s
-
Some typical DOM events1m 59s
-
Add and use event listeners6m 51s
-
-
8. Project: Typing Speed Tester
-
Rundown of HTML markup2m 58s
-
Build a count-up timer5m 56s
-
Add a reset button5m 3s
-
-
9. Loops
-
Loops3m 37s
-
Looping through arrays4m 7s
-
Break and continue loops7m 9s
-
-
10. Project: Automated Responsive Images Markup
-
Project breakdown1m 55s
-
Rundown of project setup3m 26s
-
-
11. Troubleshooting, Validating, and Minifying JavaScript
-
Troubleshooting JavaScript7m 20s
-
Online script linting5m 57s
-
Automate script linting8m 24s
-
Online script minification2m 50s
-
Automate script minification2m 24s
-
12. Bonus Chapter: Ask the Instructor
-
What are arrow functions?3m 11s
-
What does the % symbol do?3m 47s
-
-
Conclusion
-
Next Steps1m 55s
-
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.
CancelTake notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.
Share this video
Embed this video
Video: Use event listeners to detect typing