Event Listeners in JavaScript | JavaScript Events Tutorial

  Рет қаралды 21,881

Dave Gray

Dave Gray

Күн бұрын

Web Dev Roadmap for Beginners (Free!): bit.ly/DaveGrayWebDevRoadmap
In this video we'll learn about Event Listeners in JavaScript with addEventListener. We'll also explore Event Bubbling, Event Capturing, Event Propagation, and Prevent Default Behaviors. We'll look at the click event, readystatechange event, mouseover & mouseout events, and the submit event while using both named and anonymous functions as event handlers. We'll also work with the add, remove, and toggle methods of a classList. Let's get started!
▶ This video is part of the JavaScript Tutorials for Beginners Playlist found here: • Javascript Tutorials f...
✅ Quick Concepts outline:
JavaScript Event Listeners
(00:00) Intro
(0:20) Insure the DOM is available to interact with
(1:30) How to load your script for DOM interaction
(3:30) Syntax: addEventListener(event, function, useCapture)
(4:50) Using a function name in the event listener
(5:55) Removing a function with a name: removeEventListener()
(6:50) Using an anonymous function in the event listener
(8:50) Listening for the readystatechange event
(10:30) Constructing an initApp() function
(13:30) Event Bubbling
(16:00) Event Capturing
(17:00) Event Propagation and stopPropagation()
(21:45) event.target usage
(25:05) classList, add, remove, and toggle
(34:50) Listening for a HTML Form submit event
(36:20) HTML Form default behavior
(37:00) Prevent default behavior with event.preventDefault()
🚀 Source Code Files:
🔗 HTML: gist.github.com/gitdagray/7d1...
🔗 CSS: gist.github.com/gitdagray/cf6...
Note: You'll need to change display settings in the CSS for each example.
📚 Further Reading:
MDN Web Docs:
Introduction to events: developer.mozilla.org/en-US/d...
Event reference: developer.mozilla.org/en-US/d...
addEventListener: developer.mozilla.org/en-US/d...
readyState: developer.mozilla.org/en-US/d...
Eloquent JavaScript Chapter 15:
eloquentjavascript.net/15_eve...
📺 More Beginner JS Videos:
freeCodeCamp: • Learn JavaScript - Ful...
Traversy Media: • JavaScript Crash Cours...
The Net Ninja: • Modern JavaScript Tuto...
✅ Follow Me:
Github: github.com/gitdagray
Twitter: / yesdavidgray
LinkedIn: / davidagray
Blog: yesdavidgray.com
Reddit: / daveoneleven
#event #listeners #javascript

Пікірлер: 58
@9nikolov
@9nikolov 2 ай бұрын
When the "classList, add, remove and toggle" section of the video came up, I started to feel like a kid in a candy store, where everything is free. This is so exciting I can't wait to finish this JS course, then the React course and finally turn my web design ecommerce store into a reality. That will be the biggest project on my portfolio and I will utilize it to help me land a position as a frontend intern. Thank you so much for your videos Dave, I know I've said it a lot of times already, but you are truly a blessing for the web development community, your courses are accessible, of high quality, perfectly ordered and structured and well paced.
@VanPetersonKpoti
@VanPetersonKpoti 11 ай бұрын
I'm at a loss for words to express my gratitude for all the effort that has gone into bringing us this kind of exceptional, high-quality content. I've decided to follow the Full-Stack course with you. You have mastered your work. Thank you very much Mr. Dave. Your student from Togo (West Africa).
@marcusl.5785
@marcusl.5785 2 жыл бұрын
Dave, your content is highly organised, thorough, and easily digestible. I've decided to use your content as a primary source of learning, so, thanks for all your work. I predict your channel to blow up to 50k-100k subscribers within the 2022 year - I'm saying it now! What I find most appealing on your channel is not only your clear, methodical instruction, but the highly accessible/organised catalogue of content on your channel page. If I need to find instruction on a particular topic, it's not hard to find. Take care all the best.
@DaveGrayTeachesCode
@DaveGrayTeachesCode 2 жыл бұрын
Thank you, Marcus! I sincerely appreciate the kind words and prediction 🙏💯🚀
@Grihlo
@Grihlo Жыл бұрын
I'm really happy that I've found your channel. Thank you for sharing your knowledges!
@DaveGrayTeachesCode
@DaveGrayTeachesCode Жыл бұрын
You're welcome! 💯
@dansmar_2414
@dansmar_2414 2 жыл бұрын
Besides the great video you've made, the links in the description also play a huge role. Congrats and thank you so much for creating such content
@DaveGrayTeachesCode
@DaveGrayTeachesCode 2 жыл бұрын
Thank you for the kind words, Dan! 🙏💯
@bama2619
@bama2619 5 ай бұрын
Thank you, Dave! Good job. Still valuable after 3 years. Did not know that you had github link to your css in the description. I trained with my css version and it worked out.
@davidbugatti2159
@davidbugatti2159 3 жыл бұрын
You explain it really well, ty
@DaveGrayTeachesCode
@DaveGrayTeachesCode 3 жыл бұрын
Thank you!
@richardmonne7212
@richardmonne7212 2 жыл бұрын
So understandable!!! very comprehensive! Thanks a lot man! God bless you!!!!!!
@DaveGrayTeachesCode
@DaveGrayTeachesCode 2 жыл бұрын
You're very welcome! 🙏🚀
@samer3161
@samer3161 11 ай бұрын
7 more tutorials to go :p Thanks Dave!
@jasminvillatoro162
@jasminvillatoro162 2 жыл бұрын
Thank you soo much! This tutorial has helped me a lot understanding JavaScript better.
@DaveGrayTeachesCode
@DaveGrayTeachesCode 2 жыл бұрын
Great to hear! And you're welcome! 🙏🚀
@edwilsondasilva9946
@edwilsondasilva9946 Жыл бұрын
Hi, your videos are awesome! Continue please.
@DaveGrayTeachesCode
@DaveGrayTeachesCode Жыл бұрын
Will do! Thank you! 💯
@CondeAlberto
@CondeAlberto 2 жыл бұрын
I agree with Dan below, the links you provide are golden. I learned some Sass as well in this video.
@DaveGrayTeachesCode
@DaveGrayTeachesCode 2 жыл бұрын
Thank you, Alberto! The traditional academic in me insists on the references 😀
@Antonio_shortz
@Antonio_shortz 2 жыл бұрын
Thanks for making this video. I'm still a bit stuck but have a better understanding
@DaveGrayTeachesCode
@DaveGrayTeachesCode 2 жыл бұрын
Glad I could help 💯
@mostafasalaheldin812
@mostafasalaheldin812 2 жыл бұрын
Thanks for the hard work
@DaveGrayTeachesCode
@DaveGrayTeachesCode 2 жыл бұрын
Much appreciated 🙏
@saeed7996
@saeed7996 2 жыл бұрын
TOTALLY AWESOME VIDEO!!! Top-quality! It's my first time on your channel, and I'm so impressed, you explain everything so clearly and beautifully, Thank you so much!
@DaveGrayTeachesCode
@DaveGrayTeachesCode 2 жыл бұрын
You're welcome! 💯
@user-mv2ec9iv5z
@user-mv2ec9iv5z 11 ай бұрын
Around 18:00, while changing useCapture true or false, iI think the events change outwards only in both the cases from h2 to section. like while useCapture is true in all three (view, div, h2) -> events changes from h2 to section, i.e. outwards. you referred it should be inwards.
@jiweihe3413
@jiweihe3413 2 жыл бұрын
Your videos are great! For beginners like me to follow. Does the event propagation only work if click is on the innermost element? 16:30
@DaveGrayTeachesCode
@DaveGrayTeachesCode 2 жыл бұрын
It needs to be a child to bubble up for example but it does not have to be the innermost child element.
@__zenon
@__zenon 11 ай бұрын
Hey Dave, you seem to prefer using arrow functions? Is there a reason for that and is there a difference to using the other ways of writing a function? Thanks.
@DaveGrayTeachesCode
@DaveGrayTeachesCode 11 ай бұрын
Arrow functions are considered function expressions. I do like arrow functions. It is good to know both. Lately, when working with Next.js, I have found myself writing more traditional functions with the function keyword again.
@redMaple_QC
@redMaple_QC Жыл бұрын
This one is a little confusing. Codes don't match your result. I get view1 while you start on view2.
@dannyvista533
@dannyvista533 2 жыл бұрын
Hello Dave, hope all is well. I'm new to Javascript and I'm wondering what does the view.querySelector and div.querySelector means compare to document.querySelector?
@DaveGrayTeachesCode
@DaveGrayTeachesCode 2 жыл бұрын
Hi Danny, the tutorial right before this one in my Javascript for Beginners series covers how these selectors are created: kzbin.info/www/bejne/jZOqaWmpgslmibc
@dannyvista533
@dannyvista533 2 жыл бұрын
@@DaveGrayTeachesCode Thank you very much for responding Dave! I got to watch that video to learn more about the DOM. More power to your channel.
@laudlogo7057
@laudlogo7057 2 жыл бұрын
The source codes files are not what is exactly in the video. That said it is a very good beginners series. I wondering which projects to try out after this the series, i really want to practice this.
@DaveGrayTeachesCode
@DaveGrayTeachesCode 2 жыл бұрын
Thank you, Laud! I have a few projects on my channel for Vanilla JS. 💯
@bnmbnm7278
@bnmbnm7278 2 жыл бұрын
22:09 Can you explain why the h2 background color is purple and the div is also purple? I don't understand Because sometimes I can't read the subtitles
@DaveGrayTeachesCode
@DaveGrayTeachesCode 2 жыл бұрын
We are attaching event listeners and listening for a click on these elements. When clicked, we are changing the background color. Just after 21:00, we change the target from the view variable to the event.target. This begins on line 17. I really cannot explain the video in the comments as well as simply suggesting to watch the video again. Follow what I do in the code.
@omcinc
@omcinc Жыл бұрын
Hi Dave, I can't seem to load/display "My 2nd View" as seen in the beginning of your tutorial. I copied the source documents and this loads View1. I tried changing css views from flex to none - view1 disappeared, but view 2 does not appear.
@DaveGrayTeachesCode
@DaveGrayTeachesCode Жыл бұрын
Go back over the video again. I show how to achieve this.
@OlaRsk
@OlaRsk 3 ай бұрын
Which part of the video please ? ​@@DaveGrayTeachesCode
@noonecares6816
@noonecares6816 Жыл бұрын
Sorry where did you get document.queryselector("h2 +div") there is no class for h2 and div.
@DaveGrayTeachesCode
@DaveGrayTeachesCode Жыл бұрын
No class needed. You can select HTML elements, too. Any selector you might use in CSS could be a selector here.
@noonecares6816
@noonecares6816 Жыл бұрын
@@DaveGrayTeachesCode Understood, thanks.
@plamenpetkov746
@plamenpetkov746 2 жыл бұрын
Is there a repo for this lesson?
@DaveGrayTeachesCode
@DaveGrayTeachesCode 2 жыл бұрын
No repo for this one. I didn't create many for the beginning JS tutorials. I think it helps students when they type it out themselves. Thanks for asking!
@plamenpetkov746
@plamenpetkov746 2 жыл бұрын
@@DaveGrayTeachesCode I was just searching for the Scss files since I still don't know it because its little different from the ordinary CSS :D. Thanks :)
@DaveGrayTeachesCode
@DaveGrayTeachesCode 2 жыл бұрын
I have created a gist for the HTML - gist.github.com/gitdagray/7d1676f2503c987fe22e2a8c10926426 and a gist for the CSS - gist.github.com/gitdagray/cf6256c62fad399e056d8e7187950c73 . You'll need to change display settings for each example.
@adeoluadeyinka6542
@adeoluadeyinka6542 Жыл бұрын
@@DaveGrayTeachesCode these links are not working
@DaveGrayTeachesCode
@DaveGrayTeachesCode Жыл бұрын
@@adeoluadeyinka6542 they do now. KZbin was adding the closing ) to the links so I removed the ( ) surrounding the links.
@sparshverma1369
@sparshverma1369 2 жыл бұрын
Can I get the code of those sass files ?
@DaveGrayTeachesCode
@DaveGrayTeachesCode 2 жыл бұрын
I have created a gist for the HTML (gist.github.com/gitdagray/7d1676f2503c987fe22e2a8c10926426) and a gist for the CSS (gist.github.com/gitdagray/cf6256c62fad399e056d8e7187950c73). You'll need to change display settings for each example.
@sparshverma1369
@sparshverma1369 2 жыл бұрын
@@DaveGrayTeachesCode Thanks
@jperfection
@jperfection 3 жыл бұрын
You could use this instead of setting it on each side: event. target. textContent = (myText === "My 2nd View") ? "clicked" : "My 2nd View";
@DaveGrayTeachesCode
@DaveGrayTeachesCode 3 жыл бұрын
True. Not 100% sure if that would be more readable for beginners (target audience of these videos), but yes, I could have.
@user-jl6iz6ij6m
@user-jl6iz6ij6m Жыл бұрын
Please use better sound. Thanks
@DaveGrayTeachesCode
@DaveGrayTeachesCode Жыл бұрын
I currently do. Note you made your comment on a video that is over two years old. I have continued to improve over that time.
JavaScript LocalStorage and Session Storage API Tutorial
19:01
JavaScript DOM Tutorial | Document Object Model in JavaScript
35:39
Stupid Barry Find Mellstroy in Escape From Prison Challenge
00:29
Garri Creative
Рет қаралды 13 МЛН
Кәріс өшін алды...| Synyptas 3 | 10 серия
24:51
kak budto
Рет қаралды 1,3 МЛН
🍕Пиццерия FNAF в реальной жизни #shorts
00:41
Learn JavaScript Event Listeners In 18 Minutes
18:03
Web Dev Simplified
Рет қаралды 567 М.
Event Listeners - Javascript - Tutorial 14
13:55
Giraffe Academy
Рет қаралды 63 М.
addEventListener() - Beau teaches JavaScript
5:32
freeCodeCamp.org
Рет қаралды 144 М.
Top 10 CSS One Liners That Will Blow Your Mind
13:34
developedbyed
Рет қаралды 906 М.
How to accept JavaScript USER INPUT in 5 minutes 💬
5:22
Bro Code
Рет қаралды 44 М.
Javascript Objects Explained | Javascript Objects Tutorial
23:17
#miniphone
0:16
Miniphone
Рет қаралды 3,1 МЛН
keren sih #iphone #apple
0:16
kadangaruan
Рет қаралды 1,7 МЛН
One To Three USB Convert
0:42
Edit Zone 1.8M views
Рет қаралды 438 М.
How charged your battery?
0:14
V.A. show / Магика
Рет қаралды 6 МЛН