Drag and Drop & HTML APIs
HTML comes with a set of built-in browser APIs that pair markup with a little JavaScript: drag and drop, editable content, storing data, geolocation and more. This lesson tours the ones most closely tied to HTML attributes and elements.
Drag and drop
draggable="true"makes an element draggable (links and images are by default).dragstartstores data about what's being dragged indataTransfer.- A drop target must call
preventDefault()indragoverto allow dropping, then handles thedropevent.
Drag and drop is mouse-centred: it doesn't work on most touch screens without extra code and isn't keyboard-accessible. Always provide another way to do the same thing, such as "Move to Done" buttons.
Dropping files
Files dropped from the computer appear in dataTransfer.files. The file input next to it is the accessible fallback.
contenteditable
Any element can become editable. Rich text editors are built on this, though a full editor needs a lot more code to be robust.
Other HTML-related APIs
| API | What it does |
|---|---|
| Web Storage | localStorage and sessionStorage save small amounts of data in the browser |
| Geolocation | The user's location, with permission |
| Clipboard | Copy and paste text and images from script |
| Fullscreen | element.requestFullscreen() for videos, games, presentations |
| Web Share | navigator.share() opens the device's share sheet |
| Intersection Observer | Know when an element scrolls into view |
| Web Workers | Run JavaScript in the background without freezing the page |
| Server-Sent Events, WebSockets | Live updates from a server |
The JavaScript browser APIs lesson demonstrates several of these.
Built-in behaviours worth knowing
spellcheckandtranslatecontrol spelling checks and automatic translation.accesskeysets a keyboard shortcut — use with care, as shortcuts can clash with browsers and screen readers.hidden="until-found"hides content but lets find-in-page reveal it.inertmakes a whole section unclickable and unfocusable — useful behind custom overlays.
Try it yourself
Edit the shopping list, then drag items to reorder them: