I have a series of checkboxes on a dialog box. I want to loop through the controls and for each checkbox, get the name of the checkbox. I can do the loop portion, but can I query the name property of a control? It seems that the name property returns undefined, even if I had set it when I created the control. Thanks in advance.
Rick Quatro
Rick, you probably had set a .name property and didn't get an error -- a Javascript oddity. It's usually perfectly valid to do so but because checkboxes are stored internally, custom properties do not get saved.
Perhaps you want to explain why you need a "name". Are your checkboxes not saved in common variables on creation?
My dialog box has 28 checkboxes on it, so I was going to loop through them to pick up the value of each one when the dialog box is dismissed. I was going to use an array of objects, where the checkbox name would be the object's property and its value would be the object's value. I thought that since buttons could have a name property that checkboxes could as well. I am open for suggestions on a good algorithm to pick up and store each checkbox's value. Thank you very much.
Rick
var myDialog = new Window("dialog","Form");
var cbx = myDialog.add("checkbox",undefined,"Select Me",{name:"cbx"});
myDialog.show();
alert(myDialog.findElement("cbx"));
alert(cbx.name);
Here is a small sample. When I add a checkbox, I can set the name (line 2). Line 4 shows that I can retrieve the control using the name property. However, given the checkbox object, I can't get its name (line 5); it returns "undefined". It seems strange that I can't get a control's name after I set it.
Rick
Rick,
This:
var cbx = myDialog.add("checkbox",undefined,"Select Me",{name:"cbx"});
defines a creation property, which is a special kind of property for ScriptUI internal business. You can't tinker with it. To create a custom property, set it separately:
var cbx = myDialog.add("checkbox",undefined,"Select Me",{name:"cbx"});
cbx.label = 'cbx';
Like Jongware, I'm wondering why why you want to loop through labels to find checkboxes which are defined as variables. If you don't want to use variables and want to use labels to find checkboxes, you could do something like this:
var cbx = myDialog.add("checkbox {text: 'Select Me', label: 'cbx'}");
the do
for (var i = 0; myDialog.children.length; i++)
{
if (myDialog.children[i].label == 'cbx')
// . . .
}
Or maybe you could use the checkbox's text:
for (var i = 0; myDialog.children.length; i++)
{
if (myDialog.children[i].text == 'Select Me')
// . . .
}
and forget about custom properties altogether.
Peter
Hi Peter,
Thanks for the clarification on creation properties and for showing me how to use a custom property. My script is going to read an XML file to get information for the dialog's controls, so that they can be easily localized. I am also going to write the last state of the checkboxes to the XML file so they can be defaults the next time the script is run. I suppose I could just loop through the checkbox variables, but with so many checkboxes, I want to use a loop to create them as well. Adding a convenient name or label property and retrieving each control's values seems easier than looping through the variables and using a bunch of if statements. Once I get it sorted out, I will post my solution. Thanks again for the generous help.
Rick
I wrote
If you don't want to use variables and want to use labels to find checkboxes, you could do something like this:
var cbx = myDialog.add("checkbox {text: 'Select Me', label: 'cbx'}");
But that should have been
myDialog.add("checkbox {text: 'Select Me', label: 'cbx'}");
The whole point being, of course, that if you cycle through controls there's no need to declare them as variables.
Peter
frameexpert wrote:
var myDialog = new Window("dialog","Form");
var cbx = myDialog.add("checkbox",undefined,"Select Me",{name:"cbx"});
myDialog.show();alert(myDialog.findElement("cbx"));
alert(cbx.name);
...I can't get its name (line 5); it returns "undefined". It seems strange that I can't get a control's name after I set it.
Rick
all ScriptUI objects have a "properties" Property, there's no usage sample in the Guide...to get the check's name use:
alert(cbx.properties.name);
That works only for creation properties. After all, you use "properties" to set creation properties in this (alternative) format:
var cbx = myDialog.add("checkbox {text: 'Select Me', label: 'xyz', properties: {name: 'cbx'}}");
This works: alert(cbx.properties.name);
This throws an error: alert(cbx.properties.text);
Peter
I wanted to follow up with some code so you can see why I wanted to use a "name" property for my checkboxes instead of storing a bunch of checkbox variables. I have a script and cfg (XML) file that should go in the same folder. When you run the script, it reads the XML file and populates the dialog box based on the content of the XML file. When you dismiss the dialog box, it updates the checkbox values in the XML file so that they can be used as defaults the next time the script is run. This will also facilitate localization, since the checkbox labels are also read from the XML file.
Note that I need to clean things up with functions and error checking, etc., but you should get the general idea. I am definitely interested in any feedback or suggestions. You can get the code here:
http://www.rickquatro.com/resources/DialogCode.zip
Thank you very much.
Rick
North America
Europe, Middle East and Africa
Asia Pacific