Monday, March 30, 2009

JavaScript

How to set Select a particular value when Select box is loaded?

1.Find Length of Select Box.
document.mtstafftransferform.qualification.options.length
2.Loop through
a)get the value of combobox
var val = document.mtstafftransferform.qualification.options[i].value;
b)match option value with value whihc needs to selected.
3.If matches
select the value selected
document.mtstafftransferform.qualification.selectedIndex=i;


Example :

<SCRIPT language=JavaScript type=text/javascript>
var qualify ="MATHS";
var comp_len = document.mtstafftransferform.qualification.options.length;
for (i=0; i<comp_len; i++)
{
var val = document.mtstafftransferform.qualification.options[i].value;
alert(""+val);
if(val == qualify)
{
document.mtstafftransferform.qualification.selectedIndex=i;
}
}
</SCRIPT>

No comments: