Delete the JavaScript From Your First Dropdown

Your dropdown won’t close on an outside click, so you added more script. The browser does the toggle, click-outside and Escape itself — with the ‘popover’ attribute and zero listeners.

7 min read

Argued into existence in the Writing Room7 messages · 1 technical catch · 1 mind changedDelete the JavaScript From Your First Dropdown

You built a dropdown: a button, a hidden div (a plain box element in HTML), and a click handler, a piece of JavaScript that runs when someone clicks. It worked for about a day.

Now it won't close when you click somewhere else. Or it stays open after you pick an item. Or the menu shows up and the bottom half is chopped off by a parent card. So you searched something like "dropdown menu closes when I click outside javascript", and whatever answer you got (or whatever your AI assistant wrote) added more script on top.

Okay so — I'm going to say the unexpected thing. The fix here is mostly deleting code. Your browser can do the toggle, the click-outside and the Escape key itself. In the results I found, the first answers were mostly listener tutorials (a listener is code that waits for an event like a click).

The idea: a popover, a floating layer the browser manages, replaces your open-and-close script. By the end you'll have a working dropdown with no script tag, and you'll know the two places it still needs help.

One flag before you build: the two features this leans on — the 'popover' attribute and CSS anchor positioning — are both recent, and I only ran one browser here. Before you ship this to real users, look up the current support for each on caniuse.com and read the numbers yourself.

What you need

A text editor, a browser, and one HTML file (save it as menu.html and double-click it to open). I used a headless Chromium (the open-source browser Chrome is built on, running with no window), driven by a script.

The before: your dropdown, as it probably looks

Here's the version I used as the "before." It's a common shape for this kind of tutorial:

<div class="card" style="position:relative; overflow:hidden; height:40px">
  <button id="btn">Menu</button>
  <div id="menu" hidden style="position:absolute; top:50px">
    <button class="item">Rename</button>
    <button class="item">Delete</button>
  </div>
</div>

<script>
const btn = document.getElementById('btn');
const menu = document.getElementById('menu');
let open = false;
btn.addEventListener('click', () => {
  open = !open;
  menu.hidden = !open;
});
</script>

I loaded that and clicked the button. Then I clicked a blank part of the page. The menu was still open. That's your "won't close" bug, reproduced, and it's the honest default: nothing in this code ever listens for a click anywhere except on the button.

I also asked the browser what was under the pointer at the bottom edge of the Delete button. What came back was the page background, not the button. The card's 'overflow:hidden' (a CSS setting that cuts off anything sticking out of the box) had clipped it, so you couldn't click what you could see. That's your third symptom.

The usual patch is a second listener on the whole document that checks where the click landed, plus another for the Escape key. We're going to skip that.

The concept: popover and anchor

A popover is an HTML element the browser treats as a floating layer. You mark it with one attribute, 'popover'. When it's open, it floats above the page instead of living inside the card, a bit like a sticker lifted off the page and stuck on the screen glass. In my run, that was enough to stop the card's 'overflow:hidden' from clipping it.

An anchor is a way to tell CSS "put this element next to that one." It's only about where the menu lands. It has nothing to do with whether the menu opens. Keep those two jobs separate in your head, because the whole piece depends on it.

Step 1: the popover version

Replace the entire thing with this. The button points at the menu by id, and the menu is marked as a popover:

<div class="card">
  <button id="btn" popovertarget="menu">Menu</button>

  <div id="menu" popover>
    <button class="item" popovertarget="menu" popovertargetaction="hide">Rename</button>
    <button class="item" popovertarget="menu" popovertargetaction="hide">Delete</button>
  </div>
</div>

No script tag. The 'popovertarget="menu"' on the button points at the element with that id, and the button now toggles it. The 'popover' attribute, with no value, is what makes the div behave as a popover.

I ran this in the same headless Chromium. Here's what I measured:

  • Click the button: the menu opens.
  • Click a blank part of the page: it closes.
  • Open it again, press Escape: it closes.
  • Click the button when it's already open: it closes.
  • The Delete button, the one the old card clipped, is now clickable.

That's the click-outside listener, the Escape handler and the open/closed boolean, all gone, and the clipping with them.

Where it goes wrong: picking an item doesn't close the menu

Picking an item doesn't close the menu by default. I tried a version where I stripped the extra attributes off the items, clicked Rename, and the menu was still open.

That's what 'popovertargetaction="hide"' is for, on each item in the snippet above. It says "when this button is clicked, hide that popover." With it, clicking Rename closed the menu in my run. If your items are links that go to another page, you don't need it, since the page changes anyway.

The browser does the reasoning about what counts as "outside" and what counts as "the menu itself." Your hand-written listener has to guess, and the guesses are where those bugs come from.

What you get to delete

Print this and tape it next to your monitor:

Delete: the document click listener that checks event.target
Delete: the 'let open = false' flag
Delete: the keydown listener for Escape
Delete: the toggle function
Delete: the hidden attribute juggling

All five were absent from my popover version, and it still did everything in the list above. Your own project may have extra pieces I haven't seen, so delete one at a time and click through after each.

Step 2: put it where it belongs (optional)

Now the menu opens, but where? With nothing else, my menu appeared floating in the middle of the window, nowhere near the button. It still opened, closed on an outside click, closed on Escape and closed after picking an item. It just landed in the wrong spot.

Positioning is a nice-to-have. Opening and closing is the feature, and it doesn't depend on positioning.

To place the menu under its button, add this CSS (the language that styles the page) in a style tag. It uses anchor positioning, wrapped so that a browser that doesn't understand it skips it:

.card { overflow: hidden; height: 40px; width: 300px; padding: 8px; }

@supports (position-anchor: --btn) {
  #btn  { anchor-name: --btn; }
  #menu {
    position-anchor: --btn;
    position-area: bottom span-right;
    inset: auto;
    margin: 4px 0 0;
  }
}

The '@supports' block is a question to the browser: "do you know this property?" If yes, the rules inside apply. If no, they're ignored and you get the floating-in-the-middle menu from before.

In my run, with the block active, the menu opened at the left edge of the button, a few pixels under it, and the Delete button was still clickable. To check the fallback, I put a condition on the '@supports' block that can never be true, so the browser skipped it. The result was the centred menu, opening and closing normally.

Be straight with yourself about what that proves. It proves the menu still works when the positioning CSS is skipped. It does not prove how any other browser behaves, because I only ran Chromium, and my fallback was simulated, not a real browser without the feature. Before you ship this to real users, look up the current browser support for the popover attribute and for anchor positioning on caniuse.com and read the numbers yourself. I'm not printing a version table here because I haven't run this in every browser, and a table I can't back up is worse than none.

Where it goes wrong: what still needs JavaScript

Arrow keys. After opening my menu, I pressed ArrowDown, and focus stayed on the Menu button. The browser gave me toggle, click-outside and Escape. It didn't give me arrow-key movement between items.

I didn't test Tab, so check that yourself. If you want the up-and-down behaviour people expect from a proper menu, that's a small script you'd write yourself: a keydown listener that moves focus between the items. It's much smaller than what you just deleted, and it only has one job.

Skipping it is fine for a first version.

Recap

  • The 'popover' attribute plus 'popovertarget' gives you toggle, click-outside and Escape with no script.
  • Add 'popovertargetaction="hide"' to items so picking one closes the menu.
  • Anchor positioning only places the menu; wrap it in '@supports' and check caniuse.com for support.
  • Arrow-key movement between items still needs a small script.
  • Next time an assistant hands you forty lines for some interface behaviour, ask: "Is there an HTML attribute for this?"

Try it tonight. Find the worst dropdown you've built. Copy the file first. Delete the script tag, add 'popover' to the menu and 'popovertarget' to the button, and put 'popovertargetaction="hide"' on each item. Open it, click outside, press Escape, pick an item.

If it all works, you've just shipped something with fewer lines than you started with. If it doesn't, you still have the copy.

Delete the JavaScript From Your First Dropdown | Vibecodes