SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Member Space Goat's Avatar
    Join Date
    Sep 2005
    Location
    Washington, D.C.
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Options object of select menu -> "no properties" ?

    I'm writing a form that uses JavaScript to pre-fill elements with cookie values. I have it mostly working, but I can't get the code to pre-choose select menu entries to work.

    Here's my JS code:

    Code JavaScript:
    function cookieMake () {
     
        var formelms = new Array();
     
        formelms[0] = 'input';
        formelms[1] = 'textarea';
        formelms[2] = 'select';
     
        for (b=0; b < formelms.length; b++) {
     
            var tag = formelms[b];
            var elms = document.getElementsByTagName(tag);
            var count = elms.length;
     
            for (var i=1; i<count; i++) {
     
                var item = elms.item(i);
                var title = item.name;
                var type = item.type;
                var value = item.value;
     
                if (tag == 'input') {
     
                    if (type == 'radio' || type == 'text') {
     
                        ints.setSubValue (title, value);
     
                    }
     
                } else {
     
                    ints.setSubValue (title, value);
     
                }
     
            }
     
        }
     
    }
     
    function cookieFill () {
     
        if (ints.cookieExists ('ints')) {
     
            var formelms = new Array();
     
            formelms[0] = 'input';
            formelms[1] = 'textarea';
            formelms[2] = 'select';
     
            for (b=0; b < formelms.length; b++) {
     
                var tag = formelms[b];
                var elms = document.getElementsByTagName(tag);
                var count = elms.length;
     
                for (var i=1; i<count; i++) {
     
                    var item = elms.item(i);
                    var title = item.name;
                    var type = item.type;
     
                    if (cookval = ints.getSubValue (title)) {
     
                        switch (type) {
     
                            case 'select-one':
                            var selectarr = document.forms['theform'].title;
                            var optscount = selectarr.options.length;
     
                            for (var a=1; a<optscount; a++) {
     
                                if (selectarr.options[a].value == cookval) {
     
                                    selectarr.selectedIndex = a;
     
                                }
     
                            }
                            break;
     
                            case 'text':
                            item.value = cookval;
                            break;
     
                            case 'textarea':
                            item.value = cookval;
                            break;
     
                        }
     
                    }
     
                }
     
            }
     
        }
     
    }

    Firefox's Firebug shoots me the error, "selectarr.options has no properties," for the line under case "select-one" in cookieFill:

    Code JavaScript:
    var optscount = selectarr.options.length;

    If I use the full name of the element instead of a variable, the same kind of error occurs.

    If I try to get the length for the select element itself, instead of the options object, the error doesn't appear; the result is just 0.

    Can anyone help me out here?

  2. #2
    SitePoint Evangelist
    Join Date
    Jul 2007
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    var selectarr = document.forms['theform'].title;
    The above code will not find the form element whose name is contained in the variable 'title'.

    Use:
    Code:
    var selectarr = document.forms['theform'][title];

  3. #3
    SitePoint Member Space Goat's Avatar
    Join Date
    Sep 2005
    Location
    Washington, D.C.
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks; that worked like a charm!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •