How can caming towns null ?

Harun Ergün 120 Reputation points
2023-01-30T06:39:03.31+00:00

When the city is selected, it comes with the town. But when the city is selected, I want the town to be empty and for the user to select it.

User's image

I want when user choose city second drowboxlist caming null

how can add ? my code is ;

For example "AYTINTEPE" will be null after user choose towns

$('#ikametSehir').on("click", function () {
           
  
            var x = document.getElementById("ikametIlce");
            $("#ikametIlce").empty();
            var a = $('#ikametSehir :selected').text();
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
1,323 questions
ASP.NET Web Forms
ASP.NET Web Forms
A part of the ASP.NET web application framework that can be used to create ASP.NET web applications.
462 questions
ASP.NET MVC
ASP.NET MVC
A Microsoft web application framework that implements the model-view-controller (MVC) design pattern.
736 questions
No comments
{count} votes

Accepted answer
  1. Lan Huang-MSFT 11,821 Reputation points Microsoft Vendor
    2023-01-30T09:18:40.2533333+00:00

    Hi @Harun Ergün,

    You just need to add an initial "select" value to a DropDownList. <asp:ListItem Text="---Select---" Value="0" />

    <script src="Scripts/jquery-3.4.1.min.js"></script>
       <script type="text/javascript">       
           $(document).ready(function () {
               $("#ikametSehir").change(function () {
                   val = $("#ikametSehir option:selected").text();
                  
                   alert(val);
                  
                   var states = document.getElementById('ikametIlce');
                   states.classList.remove("hidden");
                   
               });
           });
       </script>
        <style>
            #ikametIlce.hidden { display:none; }
        </style>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
    
            <asp:DropDownList ID="ikametSehir" runat="server" Width="145px" ClientIDMode="Static">
                 <asp:ListItem Text="---Select---" Value="0" /> 
                <asp:ListItem Value="AF">Afghanistan</asp:ListItem>
                <asp:ListItem Value="AL">Albania</asp:ListItem>
                <asp:ListItem Value="DZ">Algeria</asp:ListItem>
               
            </asp:DropDownList>
                 <asp:DropDownList ID="ikametIlce" runat="server" Width="145px" class="hidden" >
                     <asp:ListItem Text="---Select---" Value="0" />   
                <asp:ListItem Value="AF">Afghanistan</asp:ListItem>
                <asp:ListItem Value="AL">Albania</asp:ListItem>
                <asp:ListItem Value="DZ">Algeria</asp:ListItem>
                <asp:ListItem Value="AS">American Samoa</asp:ListItem>
                
            </asp:DropDownList>
            
        </div>
        </form>
    </body>
    

    asd

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    No comments

0 additional answers

Sort by: Most helpful