Beginner elements question

S

soup_or_power

Can someone please explain why getObjects2 works and getObjects1 fails?
Thank you.

function getObjects1 ( obj_name) {
var objs = eval( "document.main_form.elements[obj_name]" );
if ( objs != null && eval( "objs.length" ) == null ) objs = new
Array( objs );
if ( objs == null ) objs = new Array();
return objs;
}
function getObjects2 ( obj_name ) {
var objs = eval( "document.main_form.elements[obj_name]" );
if ( objs != null && eval( "objs.length" ) == null &&
eval("objs.type") == 'single-selection' ) objs = new Array( objs );
if ( objs == null ) objs = new Array();
return objs;
}
 
M

Michael Winter

function getObjects1 ( obj_name) {
var objs = eval( "document.main_form.elements[obj_name]" );

The use of eval here is totally unnecessary, and in every other location
in your post. The eval function has a very specific purpose: executing
an arbitrary string provided at run-time as program code. You are highly
unlikely to /ever/ need to use this function.

[snip]
eval("objs.type") == 'single-selection'

The type property will never have the value, 'single-selection'. The
expression above should be:

'select-one' == objs.type

[snip]

A more generic approach is:

function getElements(name) {
var elements = document.forms.main_form.elements[name];

if(elements && ('string' == typeof elements.type)) {
elements = [elements];
} else if(!elements) {
elements = [];
}
return elements;
}

Mike
 
S

soup_or_power

Thanks for your help. Won't eval return undef if the 'elements' doesn't
exist instead of a javascript error?
 
M

Michael Winter

Won't eval return undef if the 'elements' doesn't exist

Which 'elements'? I use that identifier twice in two different ways.
instead of a javascript error?

The eval function won't suppress errors. Both syntax and run-time errors
will result in exceptions.

The only real reason why the code I posted might error out is if there
is no 'main_form' in the document. I don't know why you'd call the
function in this case, but this exceptional condition is detectable as well.

/* group - The name of a group of form controls
* form - The name, index, or id of a form, or
* a reference to a form element
*/
function getElements(group, form) {
/* If the form argument is a string, try
* to find a form with that name or id.
*/
if(('string' == typeof form) || ('number' == typeof form)) {
form = document.forms[form];
}
/* The form variable should now be either
* an object reference or null/undefined.
*/
if(form) {
/* Find all controls within form with
* the name passed via group.
*/
group = form.elements[group];

/* If group is not null, and has a
* string property, type, it is form
* control so insert it into an array.
*
if(group && ('string' == typeof group.type)) {
group = [group];

/* If group is null, no controls were
* found so return an empty array.
*/
} else if(!group) {
group = [];
}
/* In the remaining case (not null and
* no type property), group is a
* collection so return as-is.
*/
} else {
group = [];
}
return group;
}

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top