# Plotly\_selected is not triggered on selection changes because of legend events

**URL:** <https://community.plotly.com/t/plotly-selected-is-not-triggered-on-selection-changes-because-of-legend-events/81901>\
**Category:** plotly.js\
**Created:** [January 29, 2024, 10:02am UTC](https://community.plotly.com/t/plotly-selected-is-not-triggered-on-selection-changes-because-of-legend-events/81901 "2024-01-29T10:02:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Priyadarshini\_Gopal](https://avatars.discourse-cdn.com/v4/letter/p/ecd19e/32.png) [@Priyadarshini\_Gopal](https://community.plotly.com/u/Priyadarshini_Gopal)\
**Post date:** [January 29, 2024, 10:02am UTC](https://community.plotly.com/t/plotly-selected-is-not-triggered-on-selection-changes-because-of-legend-events/81901/1 "2024-01-29T10:02:16Z")

</div>

Hello!

I am trying to perform lasso selection and want to show the number of points selected in the UI.

I can get the details of the points from `plotly_selected` event. But when I click on any legends that have associated points in the lasso region, then obviously the selection changes, but `plotly_selected` event is not triggered.

I have replicated this behavior in the [codepen](https://codepen.io/Priya_Gopal/pen/WNmEVbE?editors=1111).

Should I need to handle this behavior differently to get the selection details? Please share your thoughts on this.

---

<div class="post-metadata">

**Author:** ![Saratoga](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/saratoga/32/28634_2.png) [@Saratoga](https://community.plotly.com/u/Saratoga)\
**Post date:** [February 21, 2025, 6:21pm UTC](https://community.plotly.com/t/plotly-selected-is-not-triggered-on-selection-changes-because-of-legend-events/81901/2 "2025-02-21T18:21:37Z")

</div>

Add this

```auto
graphDiv.on('plotly_restyle', function(eventData) {
  let data = graphDiv._fullData
  let points = 0
  
  data.forEach((d, i) => {
    if(d.visible !== "legendonly") 
      points += d.selectedpoints.length
  })
  
  document.getElementById("pointsCount").value = points

  console.log("plotly_restyle is triggered");
});

```
