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

Try it yourself
Result
  • draggable="true" makes an element draggable (links and images are by default).
  • dragstart stores data about what's being dragged in dataTransfer.
  • A drop target must call preventDefault() in dragover to allow dropping, then handles the drop event.

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

Try it yourself
Result

Files dropped from the computer appear in dataTransfer.files. The file input next to it is the accessible fallback.

contenteditable

Try it yourself
Result

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

APIWhat it does
Web StoragelocalStorage and sessionStorage save small amounts of data in the browser
GeolocationThe user's location, with permission
ClipboardCopy and paste text and images from script
Fullscreenelement.requestFullscreen() for videos, games, presentations
Web Sharenavigator.share() opens the device's share sheet
Intersection ObserverKnow when an element scrolls into view
Web WorkersRun JavaScript in the background without freezing the page
Server-Sent Events, WebSocketsLive updates from a server

The JavaScript browser APIs lesson demonstrates several of these.

Built-in behaviours worth knowing

Try it yourself
Result
  • spellcheck and translate control spelling checks and automatic translation.
  • accesskey sets 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.
  • inert makes a whole section unclickable and unfocusable — useful behind custom overlays.

Try it yourself

Edit the shopping list, then drag items to reorder them:

Try it yourself
Result