How to Drag and Drop Files with Javascript | Vanilla Javascript Drag n Drop File Input

Sdílet
Vložit
  • čas přidán 16. 07. 2024
  • Web Dev Roadmap for Beginners (Free!): bit.ly/DaveGrayWebDevRoadmap
    Learn how to Drag and Drop Files with Javascript in this tutorial. We'll create a file input that will allow you to drag n drop multiple files at once for file input on an HTML page using Vanilla Javascript.
    Subscribe ➜ bit.ly/3nGHmNn
    JavaScript for Beginners: Full Course (8 hours): • JavaScript Full Course...
    How to Drag and Drop Files with Javascript | Vanilla Javascript Drag n Drop File Input
    🔗 Source Code: github.com/gitdagray/drag_dro...
    (0:00) Intro
    (1:05) HTML Structure
    (3:41) Applying CSS Styles
    (4:34) JavaScript Functionality
    ✅ Follow Me:
    Twitter: / yesdavidgray
    LinkedIn: / davidagray
    Blog: yesdavidgray.com
    Reddit: / daveoneleven
    Was this tutorial about how to drag and drop files with Vanilla JavaScript helpful? If so, please share. Let me know your thoughts in the comments.
    #drag #drop #javascript
  • Věda a technologie

Komentáře • 48

  • @DaveGrayTeachesCode
    @DaveGrayTeachesCode  Před 3 lety +1

    This tutorial shows how to drag and drop files in Javascript for a file input. You can drag n drop just one or multiple files at once. You will learn about the drag n drop events to listen for and how to use them to indicate your drop area is active and to receive the files. This tutorial expands on lessons from my 8 hour Javascript full course video found here: czcams.com/video/EfAl9bwzVZk/video.html

  • @konstantinospascal1892
    @konstantinospascal1892 Před 3 lety +2

    I remember being so overwhelmed by what i thought was "javascript" at start, then i realized the difference between the language itself and the countless WebAPI's. I never knew about FileList and File objects, that's really neat! Great video!

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Před 3 lety +2

      Glad it helped! And I can relate to exactly what you are saying. I refer to MDN daily the various APIs, events, and more. Too much to remember it all.

  • @VitoOnYoutube
    @VitoOnYoutube Před 3 lety +2

    Thank you so much for all your great videos!
    You are calm, extremely clear and structured. Highly enjoyable.
    I wish you a ton more subs!!!!

  • @tarabario
    @tarabario Před rokem

    Thanks! I was confused by the explanation on MDN and your tutorial made it clear.

  • @jeremiah869
    @jeremiah869 Před 3 lety

    You are easily the best web dev teacher on CZcams!

  • @ashlok2003
    @ashlok2003 Před 10 měsíci +1

    Thanks You for such nice tutorials these videos are very helpful to us to learn and excel in Javascript. ❤

  • @abdoelsisi47
    @abdoelsisi47 Před 9 měsíci

    amazing tutorial, thanks bro

  • @rangabharath4253
    @rangabharath4253 Před 3 lety

    Awesome as always 👍😀

  • @ez143
    @ez143 Před 3 lety +1

    Great Video!

  • @Knards
    @Knards Před 3 lety

    Great tut

  • @ahmad-murery
    @ahmad-murery Před 3 lety +1

    Hello Dave,
    I was trying hard to find something I could comment about especially after seeing the comment by Marco D.
    But finally I found one 😎
    I was suspicious about the need for calling preventDefault for all drag events, so, I looked into the MDN and found the following information about the default actions for these events:
    *dragenter*: Will reject immediate user selection as potential target element (no need to worry about)
    *dragleave*: none
    *dragover*: Reset the current drag operation to "none" (this something we need to prevent)
    *drop*: Varies (whatever it is, we need to prevent it)
    Conclusion: We can achieve the same result by just preventing default on both dragover and drop events.
    I know it's not harmful to have the other ones but at least knowing why and when to use them gives me more confident about the code.
    Thanks Dave, you're awesome

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Před 3 lety +1

      Great observations and research Ahmad! I'm glad I'm making you try hard. You always rise to the challenge! 💯🚀

    • @ahmad-murery
      @ahmad-murery Před 3 lety

      @@DaveGrayTeachesCode Thanks Dave,
      I love little challenges and I was thinking if it's possible to include some in the video so we can fork the source code and work on it
      Regards

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Před 3 lety +1

      @@ahmad-murery good idea. I'm not sure about doing that on YT because so many are at different skill levels. However, I will do that when I design a course and make it available on another platform. I plan to do that after reaching 10k subs 👍 Goals! 🚀

    • @ahmad-murery
      @ahmad-murery Před 3 lety

      @@DaveGrayTeachesCode Fair enough,
      I hope you reach 10k subs very soon,

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Před 3 lety +2

      @@ahmad-murery thank you! I was thinking about your request and was working on a larger tutorial today. I usually have some recommendations of more features to add at the end of these longer ones, too. Thanks again!

  • @UGHEMACHANDIRANR
    @UGHEMACHANDIRANR Před 2 lety

    thank you so much it helped me a lot

  • @govindjain424
    @govindjain424 Před 2 lety

    Thank you so much

  • @tillwill3232
    @tillwill3232 Před 3 měsíci

    Dave I have a question regarding Javascript drag and drop. When i drag a div what happens is that i drag a slightly transparent copy of this div while the original div remains at the starting position. I wanna somehow make it so that when i start dragging a div there is no copy of the div and the full opacity div follows my mouse cursor immedediately. I hope you understand what I mean. I couldnt figure out how this is done, maybe you can help me. Thanks for your videos anyways, are always great!

  • @johnc100
    @johnc100 Před 3 lety

    what if i insert one file and then insert another file. Will the array consist of two files ? or it will refresh and shows only one file ?

  • @giuliocasa1304
    @giuliocasa1304 Před 3 lety +2

    Little technical question: which is the difference between defer and DOMContentLoaded? You say that we put defer to make sure that the DOM has loaded and we can work with the elements of the DOM: it looks pretty much the same as DOMContentLoaded, doesn't it? But you still need to listen to that event! So which is the subtle difference? Thank you for your simple and clear explanation ;-)

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Před 3 lety +1

      Good question! addEventListener() is a function. Without getting into a full scope discussion, I prefer to avoid the global scope so I make a habit of calling my initApp function there after the DOM is loaded. Much like listening for DOMContentLoaded, defer delays processing of the Javascript until the HTML has been parsed. If you only wanted to use defer, you could call a function like initApp in the global scope, and the HTML elements would be available. Also note, defer is implied if you use type="module" as an attribute in the script tag.

  • @naveentyagifit
    @naveentyagifit Před 2 lety

    i want the dragged file as the input type file

  • @marcod.643
    @marcod.643 Před 3 lety +3

    A question: why are you using the event listener on "DOMContentLoaded" when you have already used defer? Don't they do the same thing?

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Před 3 lety +2

      That's a great question! While calling the initApp() function in the global scope should work, I always prefer to listen for the DOMContentLoaded event or the ready.state. An addEventListener() is a function, and therefore, I'm not calling my initApp in the global space. I also consider it a good habit, but that's just my opinion. 😀 Thanks for asking! 🙏💯

    • @marcod.643
      @marcod.643 Před 3 lety

      @@DaveGrayTeachesCode Thanks for the immediate anwer, very kind from you. And of course thanks for all the great videos that you are putting on CZcams.
      You really should have many more subscribers. 👍👍👍

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Před 3 lety

      @@marcod.643 thank you for the kind words!

  • @sbmahapatra
    @sbmahapatra Před 2 lety

    Do you have next part of the video? Meanin, how we can save the files we just uploaded in this video?

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Před 2 lety

      I did make a TinyJPG clone video after this one that uses this to upload to a serverless function. It really depends on what you are saving the files to. For example AWS would be different than your own server or another storage service.

  • @BradInSanDiego
    @BradInSanDiego Před 2 lety

    How is ‘e’ getting passed to handleDrop? Does eventLister set the param? Thanks

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Před 2 lety

      Events are often implicitly provided to event handlers through event listeners. This means an event listener can simply name the function to be called when the desired event occurs without passing in the event. The function will receive the event automatically.

  • @ma-ez3zh
    @ma-ez3zh Před 3 lety

    The best part of your tutorials is they almost always start from absolutely nothing! The only thing would improve the tutorials for me is providing a link to MDN or other documentation for what is in your tutorials because for some reason I have to read something as well as watch videos on it to understand it.

  • @MrAhmed9229
    @MrAhmed9229 Před rokem

    I appreciate the tutorial Dave but it would have been nice if you at least showed us how to save the dropped file into a folder or something so we can have an idea of what to do next...

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Před rokem +1

      The follow up tutorial to this one shows what I did - which is send the file to a serverless function - but you could be sending it anywhere: czcams.com/video/jEjo9UytpIc/video.html

  • @falcy2889
    @falcy2889 Před 2 lety

    Awesome tutorial, but man I'm stuck as hell....
    What I'm trying to achieve is when I hover over and let go of the file it would be put into a input file from which I can hit upload and done

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Před 2 lety

      You should be achieving that goal with this tutorial. I'm not sure where or why you are stuck? To see a project I implemented this file drag n drop for upload, check out this project tutorial where I upload images to be compressed: czcams.com/video/jEjo9UytpIc/video.html

    • @falcy2889
      @falcy2889 Před 2 lety

      @@DaveGrayTeachesCode Wow that was a quick response for sure! Man that's dedication
      So my idea is that you hover over the area which gets highlighted just like in the video after which you let go and the javascript puts the file into a HTML from which I have a working PHP code to upload it
      I'll definitely check out that video and once again awesome work and thank you so much ! Love from Hungary