Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Removing an event

From: JavaScript: Events

Video: Removing an event

Last movie we learned how to deal with a new event, the mouse over. And it was pretty similar to the click event. But there's always something unique about the individual events. So if you notice, towards the end of the movie when you roll over some of these things, you've got multiple high-resolution versions showing up. And sometimes when you rolled over these big images, you would get these other errors. Like sometimes it would give you these empty images. And you would see these errors piling up, so we need to deal with the mouse out event, and we were going to learn how to do that in this movie, with a lot of event especially an event like mouse over you going to have to worry not just about the event itself, but any related events that might come up when you work with the JavaScript file, so lets go ahead and fix this.

Removing an event

Last movie we learned how to deal with a new event, the mouse over. And it was pretty similar to the click event. But there's always something unique about the individual events. So if you notice, towards the end of the movie when you roll over some of these things, you've got multiple high-resolution versions showing up. And sometimes when you rolled over these big images, you would get these other errors. Like sometimes it would give you these empty images. And you would see these errors piling up, so we need to deal with the mouse out event, and we were going to learn how to do that in this movie, with a lot of event especially an event like mouse over you going to have to worry not just about the event itself, but any related events that might come up when you work with the JavaScript file, so lets go ahead and fix this.

I am going to add the event listener, now this time I am going to add the event listener inside this event listener. So this new event is only going to happen when a mouse over happens, so let's go ahead and do that and we'll put it right here and we will say, e.target.add event listener, and this time we're going to look for a mouse out event. And then we want to do a function right here. Now this time we're going to name this function. We're going to call it handler. And that'll be important in a minute, when we want to get rid of this new mouseout event.

I'll show you how that works in a minute. So I'm going to open up curly brackets here, and make sure I put a semicolon. Make sure I pass a false so this bubble properly. And here's what I want to do when I mouseout out of one of those thumbnails. First I need to get the element that I want to delete. So I'm going to say var the node that I want, so I'm going to say myNode. And this is going to be equal to the target parent node. And I named this event d so I want to make sure that I pass it along, d target parentNode.

And then I'm going to do a query selector to select the element that I want. What I want to get rid of is the div with the class of preview that I create. Okay and then what I want to do is remove that node so I'm going to say my node and again when I remove something I have to look for the period of that element so I just say my node.parentNode and then remove child mynode, so this is what I want removed. So let me go and save this and let's see if it that works.

Refresh this page, and when I roll over this images, only the image shows and everything looks pretty good. But there's actually a problem with this. You're not going to be able to tell, there's 19 errors happening. Let's click on those and see what they are, let's go ahead and close this. Actually, let me just go to the console itself because it'll show you something happening here. It says cannot read property of parentNode of null, let's see, let's take a look at the Elements panel, and take a look at our art on ordered list, list item and let's roll over this list item and see what's happening.

So, when is this error happening? When I roll over one of these images and I roll out. Notice that there's a lot of Errors happening. And there's actually, you can see that, the errors seem to be increasing exponentially. Let me refresh the page and I'll show this to you. So if I roll over here one time and I roll out, it won't have an error. If I roll out over it again, I get an error. And then if I do it again, roll over roll out, now I get three errors. So, what's going on? They seem to be increasing. Exponentially. What's happening here is that some of the events are still being called so what's happening is I get a mouseout event, and then the second time I roll over and I roll out, I'm getting two mouse out events.

So there's a bunch of events that are staying in the event queue. That are not being processed and that's what's causing this error. So we need to fix that by removing the events that we have in the queue. Events in JavaScript work by inserting themselves into a queue and they will remain there until they're needed. So if you don't remove some events, it's going to cause a problem like this. And it's sort of hard to figure out because it's not doing anything visually that shouldn't be working, so this all looks good. It's just that you're getting a bunch of errors and you don't even know it.

So here's how we're going to fix that, back into our code, so what we want to do here is remove the element, now I can use e.target or the word this if I want to, and say remove. The event listener, and I have to tell it what I want to remove, in this case it's going to be mouseout, and then I'm going to call the function that I created, remember I gave it a name earlier, and I didn't give this one a name, this one doesn't need one because the mouse over event doesn't need to be removed in this case.

So, we're just going to say mouseout and then call the function we created, handler. Then give it a false Boolean here, which is just going to match the same Boolean we specified down here to make sure that the event bubble properly. So all the things about the event have to match. The first part lets the function know which event you want to target so this has to match perfectly. We want to target the event that's created by this mouse over, and that's what we did to create the function right here.

And then we want to tell it what we want it to remove. So this mouseout calls this function called handler here. And that's what I need to use as the second parameter. And then the last parameter just has to match whatever we put down here. So, let's go ahead and save this, and we'll switch over. Refresh, and it's working just like before, but if we inspect element, we're not going to get any JavaScript errors whenever we roll over these elements, and that's really what we want. So, even though sometimes you don't see any errors visually with events, you've gotta be careful and always check your console.

If you didn't get rid of that element it would eventually generate some problems with memory that you may have not known but your users might definitely feel whenever a browser crashes because there's too many events in the queue.

Show transcript

This video is part of

Image for JavaScript: Events
JavaScript: Events

27 video lessons · 7036 viewers

Ray Villalobos
Author

 

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Join now "Already a member? Log in

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.


Mark all as unwatched Cancel

Congratulations

You have completed JavaScript: Events.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.