← All notes

The DOM, closures and pointer events: a drag-and-drop pattern

The Document Object Model (DOM) is how JavaScript communicates with HTML elements: when the browser loads a page, it creates a structured representation of that page in memory. [S1]

Finding and changing elements

Before making elements draggable, JavaScript needs to locate them in the DOM; you provide an ID and getElementById locates the element. [S1]

DOM manipulation is what lets content update without a page refresh or lets elements be dragged around. [S1]

Why a closure helps

A closure creates specialized functions that remember their context even after their parent function has finished. [S1]

Closures allow private variables that persist between function calls, giving each plant its own independent coordinate tracking. [S1]

A function is a self-contained block of code that performs a specific task; parameters act like placeholders for the values a caller provides. [S2]

Anonymous functions are functions without names, defined right where you need them; arrow functions use => as a shorter syntax. [S2]

Pointer events

The lesson uses onpointerdown instead of onclick because onclick cannot handle dragging, while pointer events work with both mouse and touch. [S1]

Without prevention of the default, browsers might select text or interfere with the custom dragging behavior. [S1]

The e.clientX and e.clientY properties give horizontal and vertical positions relative to the viewport. [S1]

Claims

Sources

Attribution: source authors named above; adapted summaries, not endorsed by them. MIT notices: Copyright (c) Microsoft Corporation, MIT License.

License: CC-BY-4.0 · Adapted summaries of the named sources below; wording and synthesis by Librarian.

Cite this

Permanent ID: iil:note:pb9rwrpr

Librarian. 2026. "The DOM, closures and pointer events: a drag-and-drop pattern." iinventedlight Library, iil:note:pb9rwrpr, version of 2026-11-05. https://iinventedlight.com/library/notes/js-dom-closures-pointer-drag/. License: CC-BY-4.0.

@misc{iil_note_pb9rwrpr,
  author = {{Librarian}},
  title = {{The DOM, closures and pointer events: a drag-and-drop pattern}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-05},
  url = {https://iinventedlight.com/library/notes/js-dom-closures-pointer-drag/},
  note = {Permanent ID iil:note:pb9rwrpr. License: CC-BY-4.0.},
}

Markdown

Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com.

For language models and agents: start at /llms.txt. This page in Markdown: /library/notes/js-dom-closures-pointer-drag.md.