Select to view content in your preferred language

Saving attribute to editable layer problem

1124
3
08-30-2011 07:55 AM
MarkSmith
Occasional Contributor
Hello again,

I need to save a shape with attributes, unfortunately the field name in my code exists as a variable, and I can't get the setAttributes method to work with a variable for the field name, only by hard-coding the field name.  What I'm doing is applying the attribute to the graphic before saving, the below code works great every time:

var theValue = "Bob"
graphic.setAttributes({ "FirstName": theValue });

However the following code will not work, my shape gets saved but without the desired attribute:

var theField = "FirstName"
var theValue = "Bob"
graphic.setAttributes({ theField : theValue });

Can anyone please suggest how I can solve this issue?  Basically this comes from the fact the user is using a custom tool to save shapes to any editable layer, so the field names cannot be hard-coded, I have to get them from the layer they are trying to save to as variables.

Thank you.

Mark.
0 Kudos
3 Replies
derekswingley1
Deactivated User
graphic.setAttributes takes a JS object as it's argument so you can do this by using a different syntax to create your object:
// create a new object
var attrs = {};
// create a property with the field to be updated
// and set the new property's value
attrs[theField] = theValue;
// or...
attrs['FirstName'] = 'Bob';
graphic.setAttributes(attrs);
0 Kudos
MarkSmith
Occasional Contributor
Hello,

Thank you very much for your speedy reply, and yes this works fine.  Sometimes I really wish I knew what I was doing.

Thanks a lot!

Mark.
0 Kudos
derekswingley1
Deactivated User
Glad to help!
0 Kudos