Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-08-2009, 03:15 AM   PM User | #1
codingforumsun
New Coder

 
Join Date: Feb 2009
Posts: 11
Thanks: 2
Thanked 0 Times in 0 Posts
codingforumsun is an unknown quantity at this point
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];
}
codingforumsun is offline   Reply With Quote
Old 03-08-2009, 06:01 AM   PM User | #2
codingforumsun
New Coder

 
Join Date: Feb 2009
Posts: 11
Thanks: 2
Thanked 0 Times in 0 Posts
codingforumsun is an unknown quantity at this point
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
codingforumsun is offline   Reply With Quote
Old 03-08-2009, 06:31 AM   PM User | #3
tomws
Senior Coder

 
tomws's Avatar
 
Join Date: Nov 2007
Location: Arkansas
Posts: 2,644
Thanks: 29
Thanked 330 Times in 326 Posts
tomws will become famous soon enoughtomws will become famous soon enough
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?
tomws is offline   Reply With Quote
Users who have thanked tomws for this post:
codingforumsun (03-08-2009)
Old 03-08-2009, 08:01 AM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,555
Thanks: 62
Thanked 4,054 Times in 4,023 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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;
}
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
codingforumsun (03-08-2009)
Old 03-08-2009, 02:55 PM   PM User | #5
codingforumsun
New Coder

 
Join Date: Feb 2009
Posts: 11
Thanks: 2
Thanked 0 Times in 0 Posts
codingforumsun is an unknown quantity at this point
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];
}
codingforumsun is offline   Reply With Quote
Old 03-08-2009, 04:06 PM   PM User | #6
codingforumsun
New Coder

 
Join Date: Feb 2009
Posts: 11
Thanks: 2
Thanked 0 Times in 0 Posts
codingforumsun is an unknown quantity at this point
I got it from some other. even oldpendant code also right. the options[int]
expects a int not string.
codingforumsun is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:17 AM.


Advertisement
Log in to turn off these ads.