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

Using a drop zone

From: HTML5: Drag and Drop in Depth

Video: Using a drop zone

HTML5 drag and drop has a feature that allows you to attach a payload to a drop event. In theory, this can be very convenient, but in practice it's really a mixed bag, and at least for now, I am recommending do not use this feature in production. Here's how it works. For this example, I will be demonstrating this code in the Google Chrome browser because it fully supports it as part of the spec. It's important to note that this does not work in Firefox or Internet Explorer. So let's start by taking a look at the code, and I am just going to page through it really quick for those of you who need that, and there is the top of the javascript, and there's the next page of it. Here we are actually in a start down in the HTML.

Using a drop zone

HTML5 drag and drop has a feature that allows you to attach a payload to a drop event. In theory, this can be very convenient, but in practice it's really a mixed bag, and at least for now, I am recommending do not use this feature in production. Here's how it works. For this example, I will be demonstrating this code in the Google Chrome browser because it fully supports it as part of the spec. It's important to note that this does not work in Firefox or Internet Explorer. So let's start by taking a look at the code, and I am just going to page through it really quick for those of you who need that, and there is the top of the javascript, and there's the next page of it. Here we are actually in a start down in the HTML.

There is a couple of things you need to see. First of all, in our image you will notice there is another attribute here called data value, and its value is in quote marks I Am a Rock, and that's the payload. And down in the div for dz1, you will notice there's an attribute call dropzone, and its value is the text move and a letter asked with a colon and a mime type. This says text/plain.

So keep these things in mind when we go up and take a look at the JavaScript. And all the way up at the top, you can see we have DNDType, stands for drag-and-drop type, and that's set to that same mime type, text/plain, and this is used in a few places throughout the code. And then we have these three event handlers. We have the dragstart event, the drop event, and the dragover event. And these are being handled by handleDragStart, handleDrop, and handleDragOver.

HandleDragOver is simply there to make the drop work. We saw this in our last movie, that you need to intercept the dragOver event and prevent it from taking its default action in order for the drop event event to ever get fired. The dragStart event does a couple of interesting things. The first thing it does, you notice its status message has it event.target.dataset.value. And that is actually being passed into this setData method of the dataTransfer object.

So the dataTransfer object is what allows this magic to happen, with moving the payload around. So the payload exists in this event.target.dataset.value. The value of that is the value of the data-value attribute for the object that's being drag. So it will say I Am a Rock. It will be that string I Am a Rock. And so this status message will say drag started with parentheses and then it will say I Am a Rock. So we go and look at this in the browser and here we are in Google Chrome, and I am just going to press Reload, and when I pick up the rock you'll see that message come up in the status bar there.

Drag started (I Am a Rock), and so that's this event target dataset value. The dataTransfer effect Allowed, there is different things that happen depending on what the value is and we are just leaving it at move here. It could be any of move, copy or link, and it just needs to match in the other places where it's used. And in this example, the only other place where it's used is in this dropzone attribute. So this says move and that needs to match up here where it says move in order for the whole thing to work.

Now when the drop event comes around, we are going to get that data by matching this DND type. Remember, DND type is defined up here as text/plain and that gets passed when we set the data. So the data gets set with both the type and the value and when you read it later, that type has to match in order for the same value to get passed. So the handleDrop event actually receives the data from this event.datatransfer.getdata DND type.

And when it gets that data it puts it in this variable called data and it displays in the statusMessage ('received drop: ' + data). So let's take a look at that in the browsers. So when I grab this it says I Am a Rock, and when I drop it in, it says received drop: I Am a Rock. So what we've done, and this actually seems pretty simple and it is, is we have got this state of here which says I Am a rock in the data-value attribute of the image itself, and that is getting read in the dragStart event through event.target.dataset.value.

It reads that and it sets the data in the dataTransfer object. Using the datatype, text/plain, as a key, and then the drop of it comes along and reads it using that same key, and it displays the message. So go ahead and reload, look at it again. I pick this up. It say drag started (I Am a Rock) and we drop it and it says received drop: I Am a Rock. If this looks like a whole lot of work, to do something that's relatively simple, that's because that's exactly what it is.

Additionally this breaks in other browsers. Firefox doesn't support the data value attribute at all and Internet Explorer just doesn't work right. The good news is this entire technique is also not necessary. I am covering it here because its part of the spec and it's a prominent part of the spec that they actually give a whole lot of space to in the spec and I think it's important that you know what it is in case you run across it. But I am seriously suggesting you do not use this feature. Partly because it doesn't work in the two biggest marketshare browsers, and partly because there's another way to do it that is actually a whole lot more flexible and quite a bit more simple.

And we will cover about in our next movie, how to receive drops without using a dropzone.

Show transcript

This video is part of

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

15 video lessons · 9452 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.