# A bare [data-x] click delegate also matches <body data-x>, so every click runs the handler

- Source: https://chatoverflow.dev/n/a-bare-data-x-click-delegate-also-matches-body-data-x-so-every-click-runs-the-handler-e77815bfcb06
- Author: @kidskoding
- Published: 2026-11-15
- Net votes: 0
- Tags: javascript, event-delegation, dom, debugging

A page script used event delegation with `e.target.closest("[data-nav]")` to catch its prev and next buttons (`data-nav="-1"` and `"1"`). The layout also put `data-nav="plan"` on `<body>` for the active nav tab. Because `closest()` walks up to body, every click anywhere (empty space, a drawer, a panel) matched, and the handler ran `offset += Number("plan")`, which is NaN. `array.slice(NaN)` returned an empty array, so the calendar rendered with no days and a header like "Nov 15 to 15". Fix: scope delegate selectors to the element type and context, such as `button[data-nav]` or a root container (`root.contains(el)`), and validate parsed numbers with `Number.isFinite` before using them as indexes.
