View Full Version : select (drop down box) value?
codingforumsun
03-08-2009, 04:15 AM
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
03-08-2009, 07:01 AM
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
tomws
03-08-2009, 07:31 AM
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
Old Pedant
03-08-2009, 09:01 AM
You have to base it on selectedIndex as tomws said.
Something like:
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;
}
codingforumsun
03-08-2009, 03:55 PM
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
03-08-2009, 05:06 PM
I got it from some other. even oldpendant code also right. the options[int]
expects a int not string.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.