Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i need javascript when i select option of dropdownlist then option of 2nd dropdown should be automatically selected
Posted
Comments
Thanks7872 26-Aug-13 1:42am    
Have you tried something or not?
Mann Rai 26-Aug-13 3:01am    
I have tried this code bt it is not working
plz help me


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>dropdown</title>
</head>
<script type="text/javascript">

function ChngDDl2() {
var ddlValue = document.getElementById("<%=ddl1.ClientID%>").value;
if (ddlValue == one) {
document.getElementById("<%=ddl2.ClientID%>").value = "This is aple";
}
if (ddlValue == two) {
document.getElementById("<%=ddl2.ClientID%>").value = "This is onion";
}

}


</script>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="ddl1" onchange="ChngDDl2();" runat="server">
<asp:ListItem>one
<asp:ListItem>Two

      
<asp:DropDownList ID="ddl2" runat="server">
<asp:ListItem>This is aple
<asp:ListItem>This is onion

<br />
<br />
<br />
</div>
</form>

</body>
</html>

Expand this function with your needs:-

C#
function ChngDDl2()
    {
         var ddlValue=document.getElementById("<%=ddl1.ClientID%>").value;
         if(ddlValue == 1)
         {
                 document.getElementById("<%=ddl2.ClientID%>").value="1";
         }

     }
 
Share this answer
 
Comments
Mann Rai 26-Aug-13 2:39am    
where to call chngDDl2()???
Mann Rai 26-Aug-13 2:49am    
<pre lang="xml"><asp:DropDownList ID="ddl1" runat="server">
<asp:ListItem>one</asp:ListItem>
<asp:ListItem>Two</asp:ListItem>
</asp:DropDownList>
i have this code
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<asp:DropDownList ID="ddl2" runat="server">
<asp:ListItem>This is aple</asp:ListItem>
<asp:ListItem>This is onion</asp:ListItem>
</asp:DropDownList></pre>
TrushnaK 26-Aug-13 3:56am    
on ypur first DDl OnSelectedIndexChanged Event
<asp:DropDownList ID="ddl1" runat="server" OnSelectedIndexChanged="chngDDl2()">
Mann Rai 26-Aug-13 5:36am    
it is not working plz help me
TrushnaK 26-Aug-13 5:38am    
have you checked function gets called on changing index... If not then add alert msg in that function. if alert box comes and your function not called then tell me
you have not assigned values to dropdown listitems
This is your final soln:-

XML
function ChngDDl2() {
       var ddlValue = document.getElementById("<%=ddl1.ClientID%>").value;
        if (ddlValue == "1") {
            document.getElementById("<%=ddl2.ClientID%>").value = "This is aple";
        }
        if (ddlValue == "2") {
            document.getElementById("<%=ddl2.ClientID%>").value = "This is onion";
        }

    }


<asp:DropDownList ID="ddl1" onchange="ChngDDl2();" runat="server">
         <asp:ListItem value="1" text="one"></asp:ListItem>
         <asp:ListItem value="2" text="two"></asp:ListItem>

</asp:DropDownList>

<asp:DropDownList ID="ddl2" runat="server">
        <asp:ListItem value="1" text=""></asp:ListItem>
        <asp:ListItem value="2" text=""></asp:ListItem>
 </asp:DropDownList>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900