Graphic onClick event handler

15666
4
Jump to solution
08-10-2012 01:59 PM
SethEllingson
New Contributor II
This seems like a very simple request, I want to add an individual listener for several graphics(unknown number) that are displayed on the same graphics layer.  I'm pretty new to the ESRI javascript api, but you would think that this would be simple.

To start, I am querying a rest api that returns the coordinates of the graphics I want to display.  I have no problem displaying the graphic, but can't add a listener.  Here's what I have:

polygon[id] = new esri.geometry.Polygon(new esri.SpatialReference({wkid:4326})); polygon[id].addRing(ring); var symbol = new esri.symbol.SimpleFillSymbol(); footprint[id] = new esri.Graphic(polygon[id], symbol); footprintLayer.add(footprint[id]);  dojo.connect(footprint[id], "onClick", function(evt) {      alert('It works'); });


This is currently showing a polygon, but will be replaced with a geo-referenced image.

I must be missing something, because this is very easy to do with the flex api. Any help would be appreciated!
0 Kudos
1 Solution

Accepted Solutions
derekswingley1
Frequent Contributor
The event object passed to a graphics layer's onClick handler has a property called graphic you can use to access the graphic that was clicked. Here's an example:  http://jsfiddle.net/TXHXc/ (clicking a graphic logs a message to the browser's console)

There's also a blog post discussing finding graphics under a mouse click:  http://blogs.esri.com/esri/arcgis/2010/02/08/find-graphics-under-a-mouse-click-with-the-arcgis-api-f...

View solution in original post

4 Replies
ReneRubalcava
Frequent Contributor
Try listening to the GraphicsLayer "onClick" event
http://help.arcgis.com/EN/webapi/javascript/arcgis/help/jsapi/graphicslayer.htm#onClick

The returned event object should have the graphic that got clicked.
SethEllingson
New Contributor II
Thanks for the reply, but when I add a listener to the graphic layer, the returned event object doesn't specify which graphic was clicked.  It does return an array of all the graphics on the layer, but that doesn't help very much.  Is there another way to do this?
0 Kudos
derekswingley1
Frequent Contributor
The event object passed to a graphics layer's onClick handler has a property called graphic you can use to access the graphic that was clicked. Here's an example:  http://jsfiddle.net/TXHXc/ (clicking a graphic logs a message to the browser's console)

There's also a blog post discussing finding graphics under a mouse click:  http://blogs.esri.com/esri/arcgis/2010/02/08/find-graphics-under-a-mouse-click-with-the-arcgis-api-f...
SethEllingson
New Contributor II
Thank you for those links, they helped me solve the problem.  For some reason, Firebug is not showing all of the attributes of the click event object so I was not aware of the 'graphic' attribute.  These links showed me what I needed to know, thanks!