HTML5: Drag and Drop in Depth
Illustration by John Hersey

Coding a simple event listener


From:

HTML5: Drag and Drop in Depth

with Bill Weinman

Start your free trial now, and begin learning software, business and creative skills—anytime, anywhere—with video instruction from recognized industry experts.

Start Your Free Trial Now

Video: Coding a simple event listener

The first step in implementing HTML5 drag and drop is to register an event listener. So we are going to start by making a working copy of this file right here, 02-listening-start.html. So I am going to go ahead and I am going to just drag that and hold down the Ctrl key here on Windows and you can do the same thing with the Option key on a Mac and that will make a copy, and then I am going to go ahead and rename that to make it just say -working.html.
please wait ...
Watch the Online Video Course HTML5: Drag and Drop in Depth
1h 0m Intermediate Jul 26, 2011

Viewers: in countries Watching now:

Join Bill Weinman as he shows how to make just about any web page element draggable with a combination of JavaScript and HTML5, a technique that has increased browser support and that eliminates the need for external libraries such as jQuery. The course covers how to detect drag-and-drop support in the user's browser, code a simple event listener, use a drop zone, and even receive dropped objects without a drop zone. A simple working example game, a practical implementation of drag-and-drop that can be applied to almost any web site, is also demonstrated.

Topics include:
  • Exploring the HTML/Javascript/CSS/DOM relationship
  • Detecting drag and drop support
  • Using the HTML5 draggable and dropzone attributes and onDragStart event
  • Exploring different events
  • Receiving drops with or without a drop zone
  • Creating a simple game
Subjects:
Developer Web
Software:
HTML
Author:
Bill Weinman

Coding a simple event listener

The first step in implementing HTML5 drag and drop is to register an event listener. So we are going to start by making a working copy of this file right here, 02-listening-start.html. So I am going to go ahead and I am going to just drag that and hold down the Ctrl key here on Windows and you can do the same thing with the Option key on a Mac and that will make a copy, and then I am going to go ahead and rename that to make it just say -working.html.

And so now we have this 02-listening- working.html and I am going to open that in my text editor here in Notepad++ here on this PC. You can use whatever Text Editor works for you. And now I have a working copy. So I can make changes to it, I can experiment with it, I can do all kinds of things and still be able to revert back to the original file very easily if I need to. So this is what this file looks like. I am going to scroll down here and you can see the JavaScript part and scroll down here and you can see the HTML part.

So down here in the HTML part you can see there are two images and they are wrapped in divs. There is a Rock.png and a DropZone.png image and you will notice I have the draggable attribute equals true for the Rock and false for the DropZone image. I have an id in the div class for the DropZone image that's id=dz1. That's a number 1. That's not a letter L. And the same thing for the image. It's got an id. This is in the image tag on this one and it's img1 is the id there.

So let's take a look at what this looks like in the browser. I am going to switch back over here to my file and I am going to open that in Google Chrome. And you see we have the two images. We have the Rock and we have the little DropZone image and the idea is is that I can take the Rock and I can drop it and drop it in the DropZone. And you will notice that for right now it's not allowing that because we haven't enabled the drag and drop in the JavaScript. So let's go ahead and do that. I am going to switch back over here to the text editor and I am going to scroll back up here to the JavaScript area and this is just our starting place here.

We are going to go ahead and register an event listener and type in the event listener. So first thing I am going to do is I am going to put the event listener in here. I am going to say function handleDragStart(event), and it's just going to put up a statusMessage that says ('drag started') and this statusMessage function is of course part of the bwH5 JavaScript which was described in the introduction chapter, and all that does is displays a message down here in this status message paragraph down here in the HTML.

That's the whole handler. All it's going to do is display a status message when something happens. And now we are going to go ahead and register that function as a listener. So I am going to use my element function, which is also in my JavaScript library, to get an element by id. It's just a little shorthand. And I am going to get the img1 element. That's this image down here and so once I have that image element then I can say .addEventListener.

So because this element function returns an object I can go ahead and dereference right off of the function with that dot there. And the event listener takes three arguments. The first one is the name of the event, which is dragstart. The second one is the event handler itself, which in this case we are just going to reference the function by reference and say handleDragStart. We could if we wanted to put an anonymous function here as well. And the third one is whether or not it's going to bubble and we're going to say false. It is something that it's a little bit complicated.

It has to do with how events propagate, either top-down or bottom-up, and bottom-up is the old way of doing it and top-down by some people is considered better and the problem is if you count on it being top-down, half the browsers in the world aren't going to understand you. So everybody just puts false here so that it does what it has always done, which is bubble top up. And if that doesn't make any sense to you, that's okay. Its' not really that relevant here. I am just kind of telling you what that third argument is for and why you will pretty much always see false there.

So now we've registered our event handler and we have our event handler function right here, the handleDragStart event, and so I am going to go ahead and save and reload in the browser. And now when I grab this, you see it says drag started, and that means that that event got fired because that's this message right here. So we are successfully registering an event. I mean it doesn't do much at this point, but we are successfully registering an event and that event is getting-- that function is getting called when the event fires.

The event listener model is a basic concept for using HTML5 drag and drop. Once you understand how to do this, that is, to register an event and to write a function to handle that event, then you can go ahead and implement all the events that are supported by HTML5 drag and drop.

There are currently no FAQs about HTML5: Drag and Drop in Depth.

 
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

* Estimated file size

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 ?

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 preferences from 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.

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

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.