Beginner elements question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • soup_or_power@yahoo.com

    Beginner elements question

    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.lengt h" ) == 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.lengt h" ) == null &&
    eval("objs.type ") == 'single-selection' ) objs = new Array( objs );
    if ( objs == null ) objs = new Array();
    return objs;
    }

  • Michael Winter

    #2
    Re: Beginner elements question

    On 27/05/2005 18:28, soup_or_power@y ahoo.com wrote:
    [color=blue]
    > function getObjects1 ( obj_name) {
    > var objs = eval( "document.main_ form.elements[obj_name]" );[/color]

    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]
    [color=blue]
    > eval("objs.type ") == 'single-selection'[/color]

    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(nam e) {
    var elements = document.forms. main_form.eleme nts[name];

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

    Mike

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail.

    Comment

    • soup_or_power@yahoo.com

      #3
      Re: Beginner elements question

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

      Comment

      • Michael Winter

        #4
        Re: Beginner elements question

        On 27/05/2005 20:29, soup_or_power@y ahoo.com wrote:
        [color=blue]
        > Won't eval return undef if the 'elements' doesn't exist[/color]

        Which 'elements'? I use that identifier twice in two different ways.
        [color=blue]
        > instead of a javascript error?[/color]

        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(gro up, 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

        --
        Michael Winter
        Replace ".invalid" with ".uk" to reply by e-mail.

        Comment

        Working...