subhashyadav.smec@gmail.com wrote:
where the drop down is blank and i have only item of dropdown not index
This is really very confusing, if the dropdown is blank how you want to select any thing :)
subhashyadav.smec@gmail.com wrote:
how to change selected text in dropdown list in java script at run time
For this Let’s take following example
<asp:DropDownList ID="ddlTest" runat="server">
<asp:ListItem Text="One" Value="1"></asp:ListItem>
<asp:ListItem Text="Two" Value="2"></asp:ListItem>
<asp:ListItem Text="Three" Value="3"></asp:ListItem>
</asp:DropDownList>
If you know the value in that case your can directely select the drop down like following.
document.getElementById("ddlTest").value = 2;
If you have text and you want to select the dropdown list in that case you need to loop the dropdown list as following.
<script language="javascript">
function setSelectedIndex(ddl, txt) {
for (var i = 0; i < ddl.options.length; i++) {
if (ddl.options[i].text == txt) {
ddl.options[i].selected = true;
return;
}
}
}
</script>
And you can call this method as follwong.
setSelectedIndex(document.getElementById("ddlTest"), "Two");