Assigning a value to an element

How can I assign a value “test” to a field when the field must be referenced by its field name as follows:

var fieldname = globals.gElementName
elements[fieldname].value = "test"

Line two above returns an error. What should I be using instead of “.value”?

I assume you want to change the value of the dataprovider that’s attached to this element?

controller.setDataProviderValue("myColumnName", "test")//dataprovider,value

…looking at your code more closely, this is probably the way you want to go

var fieldname = globals.gElementName 
var dataproviderName = elements[fieldname].getDataProviderID()//get name of the dataprovider attached to this element.
controller.setDataProviderValue(dataproviderName, "test")

Then probably the value of your global that goes into var fieldname doesn’t exist as an element on your form .

1)Make sure the dataprovider (eg. textfield) you are trying to set , has a name-property entered (eg. “myTarget”), in order to make it appear as an element.
2) Test your script by doing.

var fieldname = "myTarget"
var dataproviderName = elements[fieldname].getDataProviderID()
controller.setDataProviderValue(dataproviderName, "test")

Yes that works. It solves a big problem for me.
Thank you very much!