SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Evangelist jimday1982's Avatar
    Join Date
    Sep 2002
    Location
    Atlanta, GA
    Posts
    562
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Return select box selection

    I'm trying to build a function with one parameter (name of select box) that simply returns the option that was selected.

    Here's the code I have so far:

    Code:
    function getanswer(variable) {
    len = eval("document.theform." + variable + ".length");
    i = 0;
    chosen = "none";
    
    for (i = 0; i < len; i++) {
    if (document.theform.variable[i].selected) {
    chosen = document.theform.variable[i].value;
    }
    }
    //Test against answer here
    alert(chosen);
    }
    It seems to be getting hung up on the second line because the .length is not being appended to the eval for some reason. If someone could point me in the right direction, or offer some better code, I'd greatly appreciate it. Thanks a lot!
    Jimmy Day
    Senior Systems Analyst
    Piedmont Healthcare Corporation

  2. #2
    SitePoint Member rubyrock81's Avatar
    Join Date
    Mar 2008
    Location
    Seattle area
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've been having the same problem with a function of mine that references a variable with the document object. No matter how I tried it, it would not work. When I put an actual value:

    eval("document.theform.variable1.length");

    it works fine, but yes, how can you reference a variable that changes dynamically using the document object? As soon as I add the " + variable + " it does not work.

    One thing that I did notice is that you might want to make the inner quotes single instead of double, so for ' + variable + ' instead of " + variable + ". Also, I believe referencing the name of a form has been deprecated; you should access it by (document.forms[0].variable1.value);

  3. #3
    SitePoint Evangelist jimday1982's Avatar
    Join Date
    Sep 2002
    Location
    Atlanta, GA
    Posts
    562
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I played around a little more and got it to work using this bit of code:

    Code:
    function getanswer(answer) {
        var x=document.getElementById(answer)
        alert(x.options[x.selectedIndex].text)
    }
    Hopefully that will help you out as well.
    Jimmy Day
    Senior Systems Analyst
    Piedmont Healthcare Corporation

  4. #4
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There is no need to use eval here.

    You can access the select like this
    Code:
    var select = document.formName[variable];
    or pass the id to the function and use the getElementById method.

  5. #5
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    function getanswer(answer) {
        alert(document.getElementById(answer).value);
    }
    or - if you are using field names and not element ids
    Code:
    function getanswer(answer) {
        alert(document.forms['theform'][answer].value);
    }

  6. #6
    SitePoint Member rubyrock81's Avatar
    Join Date
    Mar 2008
    Location
    Seattle area
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you!

    Seems so simple, right, but I had been banging my head against the wall with this one for a while now, and now it works! Ok, it's obvious, I'm a newbie


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
  •