A common problem with ASP.NET pages, especially if you use MasterPages .
Generated pages will have their elements IDs changed.
One solution to your problem is to provide these elements with a unique class and use it in the selector instead of IDs :
<asp:DropDownList ID="ddlCity" runat="server" CssClass="stdFldWidth ddlCity" onchange="setFDFlag()"> </asp:DropDownList> <asp:DropDownList ID="ddlState" runat="server" CssClass="stdFldWidth ddlState" DataSourceID="dsUSState" DataTextField="US_State" DataValueField="State_ID" AutoPostBack="true" > </asp:DropDownList>
...
$(function () { var states = { AL: ['Birmingham', 'Montgomery', 'Tuscaloosa'], FL: ['Miami', 'FtLauderdale', 'Jacksonville'], GA: ['Atlanta', 'Macon', 'Athens'] }; function populateSelect() { $('.ddlCity').empty(); var stateSelected = $('.ddlState').val(); if (stateSelected) { states[stateSelected].forEach(function (sel) { $('<option>').val(sel).text(sel).appendTo($('.ddlCity')); }); } } populateSelect(); $('.ddlState').on('change', function() { populateSelect(); return true; }); });
Edited using the @sebnukem recommendation to save data to object , so you can get rid of eval() .
Updated by putting all the code in a jQuery event handler.
Updated by returning true in the jQuery onchange event handler for DropDownList , to continue with PostBack .
Demo
source share