# Mousedown events in the plot area

**URL:** <https://community.plotly.com/t/mousedown-events-in-the-plot-area/6329>\
**Category:** plotly.js\
**Created:** [October 17, 2017, 12:17pm UTC](https://community.plotly.com/t/mousedown-events-in-the-plot-area/6329 "2017-10-17T12:17:24Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![NickNack](https://avatars.discourse-cdn.com/v4/letter/n/5f9b8f/32.png) [@NickNack](https://community.plotly.com/u/NickNack)\
**Post date:** [October 17, 2017, 12:17pm UTC](https://community.plotly.com/t/mousedown-events-in-the-plot-area/6329/1 "2017-10-17T12:17:24Z")

</div>

I need to listen to mousedown events (not a full click) on the y-axis and perhaps the main plot area, but my events only trigger on the margins. How can I do this without killing the default Plotly interactions? In particular, can I detect mousedown on the y-axis but still keep the possibility of zooming by dragging the y-axis?

I should point to [this related post](https://community.plotly.com/t/plotly-onmousedown-and-onmouseup/4812/4), but it just confused me even more. (It made me think that mouseup needed to be put on `document`, but it works on the plot margins for me.)

I prepared [a Codepen](https://codepen.io/anon/pen/BwGvxQ) to play with.

---

<div class="post-metadata">

**Author:** ![etienne](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/etienne/32/17_2.png) [@etienne](https://community.plotly.com/u/etienne)\
**Post date:** [October 17, 2017, 9:42pm UTC](https://community.plotly.com/t/mousedown-events-in-the-plot-area/6329/2 "2017-10-17T21:42:01Z")

</div>

Here’s a hacky way around the problem: [https://codepen.io/etpinard/pen/eGbdqL?editors=0010](https://codepen.io/etpinard/pen/eGbdqL?editors=0010)

---

<div class="post-metadata">

**Author:** ![NickNack](https://avatars.discourse-cdn.com/v4/letter/n/5f9b8f/32.png) [@NickNack](https://community.plotly.com/u/NickNack)\
**Post date:** [October 17, 2017, 10:21pm UTC](https://community.plotly.com/t/mousedown-events-in-the-plot-area/6329/3 "2017-10-17T22:21:36Z")

</div>

Oooo, that is very close to a solution for me! It works for the main plot area, but for some reason it doesn’t work for the y-axis (which is where I need it). It’s strange because they seem to be nearly identical sibling elements of the drag layer, so I can’t see why one would work but not the other. Maybe another event on the y-axis is stopping event propagation? Or is there another reason?

---

<div class="post-metadata">

**Author:** ![etienne](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/etienne/32/17_2.png) [@etienne](https://community.plotly.com/u/etienne)\
**Post date:** [October 18, 2017, 1:21pm UTC](https://community.plotly.com/t/mousedown-events-in-the-plot-area/6329/4 "2017-10-18T13:21:09Z")

</div>

Here’s the complete list of drag nodes we currently use:

![image](https://us1.discourse-cdn.com/flex024/uploads/plot/original/2X/7/7e5cda83cf85eb05e772c10e1b00abfbc99718af.png)

So to detect mouse down on the y-axis, you can use something like:

```auto
Plotly.d3.select('.nsdrag').on('mousedown', () =>)

```

---

<div class="post-metadata">

**Author:** ![NickNack](https://avatars.discourse-cdn.com/v4/letter/n/5f9b8f/32.png) [@NickNack](https://community.plotly.com/u/NickNack)\
**Post date:** [October 18, 2017, 4:00pm UTC](https://community.plotly.com/t/mousedown-events-in-the-plot-area/6329/5 "2017-10-18T16:00:40Z")

</div>

What? I don’t get it … Your suggestion works, but I don’t understand why. Or rather, I don’t understand why your previous suggestion which put the listener on the `.drag` class didn’t also work on the y-axis. The y-axis also has class `.drag` (just like all nodes listed in that figure), so why do you need to write a more specific selector that targets `.nsdrag`?

In any case, thank you very much!

---

<div class="post-metadata">

**Author:** ![etienne](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/etienne/32/17_2.png) [@etienne](https://community.plotly.com/u/etienne)\
**Post date:** [October 18, 2017, 4:02pm UTC](https://community.plotly.com/t/mousedown-events-in-the-plot-area/6329/6 "2017-10-18T16:02:38Z")

</div>

```auto
Plotly.d3.selectAll('.drag').on('mousedown', () => {})

```

would also _work_.

---

<div class="post-metadata">

**Author:** ![NickNack](https://avatars.discourse-cdn.com/v4/letter/n/5f9b8f/32.png) [@NickNack](https://community.plotly.com/u/NickNack)\
**Post date:** [October 18, 2017, 4:09pm UTC](https://community.plotly.com/t/mousedown-events-in-the-plot-area/6329/7 "2017-10-18T16:09:39Z")

</div>

Oh so that’s it, `d3.select()` works like `querySelector()`! It looked so much like jquery-style chaining that I implicitly assumed that it matched all nodes and not just the first matching node. Thank you again for your help and patience!

---

<div class="post-metadata">

**Author:** ![c7h](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/c7h/32/22514_2.png) [@c7h](https://community.plotly.com/u/c7h)\
**Post date:** [September 8, 2023, 2:18pm UTC](https://community.plotly.com/t/mousedown-events-in-the-plot-area/6329/8 "2023-09-08T14:18:52Z")

</div>

Is d3 still exposed in the latest versions of Plotly? It seems that direct access [has been removed](https://github.com/plotly/plotly.js/issues/6254).
