SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Zealot
    Join Date
    Dec 2003
    Location
    Ireland
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Setting SELECT option value

    Hi All,

    In JS can you set the value of a SELECT dropdown box by text value, not index value?

    The following will set the SELECT box by the index value, selecting the second value in the dropdown box

    var varyear = 1
    document.myform.year.options[varyear].selected = true;

    however i want to set it like this...
    var varyear = 2006
    document.myform.year.options[varyear].selected = true;

    ...to set the SELECTED value to 2006...which doesn't work

    Thanks for any help

    Melt

  2. #2
    SitePoint Enthusiast scarecrow-rye's Avatar
    Join Date
    Nov 2005
    Location
    Cornwall, UK
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Might it be the case of looping through each of the SELECTs options and comparing its value to your string? Something like:

    for (i=0; i<myform.year.options.length; i++) {
    if (myform.year.options[i],value == varyear) {
    myform.year.options[i].selected = true;
    break;
    }
    }

    This is just off the top of my head and hasnt been tested.


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
  •