Skip to content

Instantly share code, notes, and snippets.

@foxnewsnetwork
Created May 20, 2015 18:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save foxnewsnetwork/3675adbf20b744fc6cf3 to your computer and use it in GitHub Desktop.
Save foxnewsnetwork/3675adbf20b744fc6cf3 to your computer and use it in GitHub Desktop.
d3 element click events not working

The Problem

You're using the d3 library, and you do something like the following:

.enter()
.append "rect"
.attr "fill", "none"
.attr "stroke", "black"
... # other stuff
.on "click", -> alert "whatsApp sucks"

And you navigate over to your rectangle in your browser, click it, and nothing happens.

Why?!

If you fill none on a svg element, all clicks (and mouseovers, mouseouts, etc.) will fall through it. This is one of those things that's obvious if you know it, but otherwise is a cause for much butt-hurt if you didn't.

The Fix

Change the fill:

.attr "fill", "white"

That's it, now your clicks will work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment