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

Exploring the different events

From: HTML5: Drag and Drop in Depth

Video: Exploring the different events

Now that we know how to register and code events let's take a look at the events supported by HTML5 drag and drop. I am going to start by making a working copy of this events-start.html and I am going to rename this copy as events -working.html and I am going to open that in my text editor. And so here's what it looks like. I am just going to scroll through real quickly here. You notice there is the head matter. It brings in the CSS and the JavaScript files and then here's the JavaScript itself.

Exploring the different events

Now that we know how to register and code events let's take a look at the events supported by HTML5 drag and drop. I am going to start by making a working copy of this events-start.html and I am going to rename this copy as events -working.html and I am going to open that in my text editor. And so here's what it looks like. I am just going to scroll through real quickly here. You notice there is the head matter. It brings in the CSS and the JavaScript files and then here's the JavaScript itself.

This is actually the place where we left off in the previous movie. And so we have the one event being handled, the dragstart event, and it's handled by the handleDragStart function. And there is the body of the HTML there and again this is right where we left off in the last movie. So let's go ahead and open this in the browser and we will open this in Google Chrome and you'll see that when I grab the Rock it says drag started and that's all it really does. So what we want to do here is we want to look at the different events and how they work.

And so the easiest way to do that is to write a generic event handler and just plug it into the different events and look at what happens. So here's our little generic event handler and it looks like this. We have a variable that counts the number of times an event is fired and you'll start at 1 and we have a simple function that will handle the event, an event handler function. So this is generic. I am just calling a handleDragEvent and the event itself, because we are not using it, I am just going to call it e and all it's going to do is display a statusMessage with the count.

And the little plus plus operator, all that does is every time the event counter is displayed it goes ahead and increments it for the next one. So it will show the next number and that's all there is to it. So that's our generic event handler. And now we just plug this in and I am just going to do that by making a copy of this one and paste in the copy, because I like copy and paste and instead of dragstart this would be for the drag event and handleDragStart will become handleDragEvent.

And so I am going to save this and all this is going to do is this is going to capture that DragEvent and for every time that DragEvent gets fired, it's going to display this counter and increment the counter so that it will display the next number, so 1, 2, 3, 4, 5, 6, 7, 8 etc. So let's go over to the browser now and reload and now when I grab this watch what happens. So now that counter-- and I am just holding it here in one place. As long as I have it dragged, as long as I have my finger on the mouse button, it's going to count and what that means is that event is getting fired over and over and over again.

So that's a good thing to know. Now we know something about the Drag event. When I drag something, as long as I have it dragged, wherever it is anywhere in the browser window, that counter is going to continue to get fired. That event gets fired over and over and over again in rapid succession as long as I have that object in its drag. So that's the drag event. Let's take a look at the dragend event. All we have to do now is change this to dragend and save and it's still using our same generic handler so that we can see what happens.

So when I switch back over hear to the browser and press Reload, now I get the drag started message. I am not getting that drag event itself anymore. Now I have a dragend event. So when I let go off my mouse button that ends the drag and I get count one and I drag it again. Now I get count tow. Every time I let go of the mouse button I get another count because that event is getting fired again. This is just a great way to look at all these different events and see how they work. The rest of the events actually happen on the destination rather than on the source.

So you'll notice our destination has a different id. This id img1 is for our source. Our destination is a drop zone. This over here is actually the whole div rather than just the image, because I am going to drop it anywhere in this div, and that has an id of dz1 for drop zone 1. And so I am going to change my element call to call dz1 and I am going to change my event to dragenter. So now when I drag the object into the drop zone and the object actually crosses the boundary into the drop zone, that's when it's going to fire the event.

So we will take a look at what that looks like. I am going to press Reload in the browser here and now I have got drag started and I am going to drag this over. You see it says count: 1 because I dragged it in and I can hold it in there and move it around and leave and come back and I get a number 2. And I leave and I come back and I get a number 3. Now if I do this really slowly, you will notice that I can get several at once. So it's not entirely reliable that it's going to be one call. It might be a few calls every time something and depending on how slowly they do it and how many times they actually cross that border on the way in.

So that's the dragenter event. Likewise there's a dragleave event. Let's save that and I am reloading the browser. And so I have dragged in and nothing happened. Now I am going to drag out and I get a 1. When I dragged in I got another 2 because my mouse must have crossed that border twice along the way. And here I can run it past and only get 1. So again you just want to get an idea of how this works so that when you write your code you can handle all of these different contingencies.

And so that's dragleave and finally dragover. You will notice that that works very much like the drag event did because you sees it says drag started. When I drag the object over the zone you will notice that the counter just keeps going. As long as I hold it over that zone, that dragover event fires over and over and over again. So that's also good to know. So there's also an event for drop and this lets you know when an object is dropped over your drop zone.

And just change this to say drop and I am going to save it and you will notice that something doesn't happen that you expect to happen. What you would expect to have happen is that when we drop the object over the drop zone is that this event will get fired. And you will notice I am going to reload this and I am going to just drop this object over the drop zone and you notice it still says drag started. We didn't get a count for that. We would expect this to be counting 1, 2, 3, 4, 5, like that and that's not what's happening and the reason for this is actually very odd.

According to the documentation, what happens is the dragover event has a default action. Each of these events has a default action. For the most part we don't care about the default action, but the default action for the dragover event actually resets the drag and the impact of this is that the drop event never gets fired and the only way to get the drop event to fire is to stop the dragover event from executing its default action. And you read all of the blogs and you read all of the commentary on this particular set of APIs and everybody scratches their head and says "I have no idea why this is the way that it is." Probably has something to do with the original implementation back at Microsoft and the person who made that decision for whatever reason isn't telling us why they made that decision.

For most of us it just seems dumb, but it's just the way that it is. So the way to handle this is to write a specific event handler for the dragover event and to intercept that default action and make it not happen. So here's how that works. It's very simple and you are just going to need to do this whenever you need to capture a drop event, which is going to be pretty much anytime you need to implement drag and drop, so you are going to do this a lot. So I am going to call this handleDragOver. I am going to say if event .preventDefault.

So if there is a method called preventDefault in the event object then we are going to call that method. Now there are some implementations which do not have the preventDefault and instead they work just by having this function return false. So this is what this looks like. It doesn't have to make any sense; you just have to know that it works. And then we need to add an event listener that calls that and so I am going to cut and paste here.

This is dragover event and it has to call our dragover and that will be on the destination like that and so we will save that. And so now what's going to happen is every time that dragover event is called, instead of resetting the drag, it's going to call this preventDefault function or return false depending on what browser we are using, and that will allow the drop event to fire.

So we will get our little counter. So what we expect to see happen is that every time we drop this rock into the drop zone we will get a count. There it is, count:1, count:2, count:3. So oddly enough, that's what it takes to make this work. You have to handle your dragover event or your drop event will never get fired. And in your dragover event you have to call this preventDefault and/or probably both of them, return false, and that will prevent the drag from getting reset and it allow your drop event to get fired.

So now we understand how all of these events work and we can take this knowledge and we can go ahead and start using them.

Show transcript

This video is part of

Image for HTML5: Drag and Drop in Depth
HTML5: Drag and Drop in Depth

15 video lessons · 9451 viewers

Bill Weinman
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 HTML5: Drag and Drop in Depth.

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.