How to get Id and value of dropdownlist of asp.net using Javascript ?


Hi

So many time we will get scenario to get Id and value of dropdownlist using Javascript in asp.net. We can get like this

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">

 <script type="text/javascript">

        function CountrySelect() {
            var dll = document.getElementById('<%=ddlCountry.ClientID %>');
            
            //For getting Text value of dropdownlist
            var mod = dll.options[dll.selectedIndex].text;
            
            //For getting Value of dropdownlist
            var mod1 = dll.options[dll.selectedIndex].value;
            alert(mod1);
            
        }

</script>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
Country Name: 
    <asp:DropDownList ID="ddlCountry" runat="server">
        <asp:ListItem Value="0">Select</asp:ListItem>
        <asp:ListItem Value="1">Nepal</asp:ListItem>
        <asp:ListItem Value="2">India</asp:ListItem>
    </asp:DropDownList>
    
    <br />
    <asp:Button ID="Button1" runat="server" OnClientClick="CountrySelect();" Text="Submit" />
</asp:Content>


Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: