Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Feb 2009
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    select (drop down box) value?

    how do I set select (drop down box) value? the following code is not working for me. thanks.


    switch (nameVal[i].toString()) { //nameVal[i].toString()--->"Co_Ordi"
    case "Co_Ordi" : document.getElementsByName("Co_Ordi")[0].options["Average"].selected=true;
    alert("CO_ORDI : "+ nameVal[i+1] ); // nameVal[i+1] --->"Average"
    break;
    default: document.getElementsByName(nameVal[i])[0].value=nameVal[i+1];
    }

  • #2
    New Coder
    Join Date
    Feb 2009
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I mean it enters the case "Co_Ordi" : but does not change the select box selection to the specified. also it shows the alert. no idea what to do.. can some one help me please

  • #3
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    You should have posted this in teh main Javascript forum if you're not addressing an issue with a specific framework.

    I thin you may be looking for the selectedIndex property. http://www.mredkj.com/tutorials/tutorial002.html
    Are you a Help Vampire?

  • Users who have thanked tomws for this post:

    codingforumsun (03-08-2009)

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    You have to base it on selectedIndex as tomws said.

    Something like:
    Code:
    function setSelectByValue( sel, val )
    {
        for ( var ix = 0; ix < sel.options.length; ++ix )
        {
            var opt = sel.options[ix];
            if ( opt.value == val ) 
            {
                opt.selected = true;
                return;
            }
        }
        // no match on value...could give error or just ignore or???
        return;
    }

  • Users who have thanked Old Pedant for this post:

    codingforumsun (03-08-2009)

  • #5
    New Coder
    Join Date
    Feb 2009
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts
    hi oldperdant

    function setSelectByValue( sel, val )
    {
    for ( var ix = 0; ix < sel.options.length; ++ix )
    {
    var opt = sel.options[ix];
    if ( opt.value == val )
    {
    opt.selected = true;
    return;
    }
    }
    // no match on value...could give error or just ignore or???
    return;
    }

    looks exactly right. but in the below code I get "Average" from database
    and that value is present in the drop down box. so will it be set with the below code? OR is the code right? coming to the above code, the for loop
    with options looks right but is it necessart to for loop it when we get the value from database.

    switch ("Co_Ordi") {
    case "Co_Ordi" : document.getElementsByName("Co_Ordi")[0].options["Average"].selected=true;
    break;
    default: document.getElementsByName(nameVal[i])[0].value=nameVal[i+1];
    }

  • #6
    New Coder
    Join Date
    Feb 2009
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I got it from some other. even oldpendant code also right. the options[int]
    expects a int not string.


  •  

    Posting Permissions

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