Results 1 to 4 of 4

Thread: Form object question about SELECT

  1. #1
    Join Date
    Aug 2007
    Location
    Kansas, USA
    Posts
    36
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Form object question about SELECT

    Can you enable a value in a form SELECT object by name?

    I tried:

    document.MyForm.MySelect['yes'].selected = true

    but I got an error.

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    <option> elements can't have names.

    Use document.forms[yourFormId].elements[yourElementId] to access an element, to minimise accidental collisions.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. The Following User Says Thank You to Twey For This Useful Post:

    DigitalBard (08-21-2008)

  4. #3
    Join Date
    Aug 2007
    Location
    Kansas, USA
    Posts
    36
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Thanks, I ended up just looping through the whole thing to select the items I needed:

    Code:
    // sSel - name of the SELECT element in your form
    // sStr - elements of the SELECT you want selected, separated by commas
    function selectFromStr(sSel, sStr)
    {
      var o = document.getElementById(sSel);
      var sel = sStr.split(', ');
      for(i=0;i<o.length;i++)
      {
        o.options[i].selected = false
        for(s=0;s<sel.length;s++)
        {
          if (o.options[i].text==sel[s]) o.options[i].selected = true
        }
      }
    }

  5. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Hmm... note that that's a name you should be passing there, not an ID.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

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
  •