SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    Knowledge is key 2 progression Tryst's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,181
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Setting a <SELECT> option as 'selected'

    Hi all,

    I have a little bit of a complex script where I have a <SELECT> that is pre-loaded with options that are loaded from a database in PHP. But, when this page is loaded the <SELECT> is disabled, and is only enabled when an onChange() is triggered on another <SELECT> that is situated on the same page.

    Anyway, when I enable the <SELECT> I would like to have one of the <OPTION>'s selected (instead of having a blank <SELECT>).

    The line of JavaScript that I have so far (but isn't really getting me anywhere) to try to select an <OPTION> when the <SELECT> is enabled is as follows...

    Code:
    document.forms['edit_consultant'].elements[con_elements[j]].options[selectedIndex].value = consultants[conid][j];
    ..where I am guessing that the 'options[selectedIndex].value' will select the <OPTION> with the value of consultants[conid][j] to be selected.

    But this is not working

    All I need to know, is how to set an <OPTION> to be selected.

    Thanks

    Tryst

  2. #2
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,236
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Code:
    document.forms['edit_consultant'].elements[con_elements[j]].options[selectedIndex].value = consultants[conid][j];
    This code you've posted will change the value of the currently selected option, not select a new one. You'll have to do something like this to get the effect you want:
    Code:
    document.forms['edit_consultant'].elements[con_elements[j]].options[consultants[conid][j]].selected = true;

  3. #3
    Knowledge is key 2 progression Tryst's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,181
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks vgarcia, thats solved it.

    Good job you mentioned the point that may code would have changed the value of the <SELECT> - that would have put some inconsistencies in my database

    Tryst


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
  •