I am able to obtain the features of the layer which is selected by the user in the Layers table and then intersected by a user defined polygon, using e.features. I was able to obtain the attribute values of the feature as a property, like this e.features[0].attributes.Family or e.features[0].attributes.Species. The e.features[0].attributes object looks like this, Object { Family="Pinaceae", Species="Pinus cembroides"}.

Now the problem that I am facing is when a user selects a different layer, say states, the attributes properties change, the object now looks like this, Object { STATE_NAME="Arizona", STATE_FIPS="04", SUB_REGION="Mtn", more...}. So now I need to fetch the attribute data by again changing the properties, like e.features[0].attributes.STATE_NAME.

So I am trying to create a common function where I can obtain the properties of the attributes object and then directly fetch the values for any layer. This is the code that I am trying to implement,

var attrData = [];
for (var i = 0; i < e.features.length; i++) {
    //Work on how to fetch properties for all the layers attributes
    feat = e.features[i];
    for (prop in feat.attributes) { alert(prop); } //this gives all the property values
    var values = "";
    for (prop in feat.attributes) {
      value = (value === "") ? feat.attributes.prop : value += feat.attributes.prop; //But feat.attributes.prop is undefined


As you can see, for (prop in feat.attributes) { alert(prop); } gives me all the property names but when I assign this prop to feat.attributes.prop to fetch the value, it returns undefined.

Not sure where am I going wrong? This is my application, Click on the 'draw poly' in the toolbar and then select 'Pima Pine' layer in the Map Layers column and create a polygon and intersect it with the blue stars (Pima Pine), you should get the table with the info of the stars which intersect with the vector polygon. Now I am trying to replicate the same for the USA States and that is where I am failing.

How should I change the code to fetch the attribute values for all the layers?

  • Sam, I am needing to do something you did. I would like your guidance. I need to get the features of a polygon created by the user. These features appear under the active / visible layers. You did it using something besides OpenLayers? Thank you in advance.
    – user26081
    Jan 22, 2014 at 15:05
  • 1
    This does not appear to be an answer to the current question. If you have a new question you should post it again, and link back here if you think it's related.
    – Martin
    Jan 22, 2014 at 15:23

1 Answer 1


You should write:

  • Thanks, did not think it would be that easy. :)
    – Sam007
    Sep 27, 2012 at 16:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.