javascript - Setting default text of select box depending on another select box -
i have question regarding ability set default text of selectbox depending on default text of selectbox.
i trying populate country , state drop down list, state options change in regards country have chosen. however, when no country chosen, state selectbox "please choose country". right says "state" otherwise.
in other words, if country select box says "country" want state select box "please choose country". , once user chooses in country selectbox, state selectbox won't say "please choose country" anymore.
simple example of have
country <.select id="country" name ="country">
state <.select id="state" name ="state">
i inexperienced programmer not sure functions can possibly used. accomplished jquery? appreciate in regards matter , hope question wasn't confusing. thank you.
yes can accomplish using jquery.
check out plunker created below
https://plnkr.co/edit/v2vcu20olauex8ibtkc2?p=preview
<select id = "country" onchange="func()"> <option value="">select</option> <option value="usa">usa</option> </select> <select id = "state"> <option value="new"> please select country</option> </select>
in script file.
var func = function(){ var state = { 'abc' : 'abc', 'def' : 'def' } var sel = $('#state'); $.each(state, function(key, value) { $("#state option[value='new']").remove(); sel.append('<option value="'+value+'">'+key+'</option>'); });};
Comments
Post a Comment