How to get trace legend when click on the plot

I’m trying to get the trace legend, when click on the plot using js.
I can get the x, y value using the following code, how do i get the legend from data? i’v tried to use data.name, data.points[0].name but none of them are working. Does any one know if it’s possible to do it and how to do it?

plotDiv.on(‘plotly_click’, function(data){
xVal = data.points[0].x ;
});

Thanks in advance!

here’s how: http://codepen.io/etpinard/pen/dpkyAr

Thank you so much ! it works :slight_smile: