javascript - Drop down value depends on radio button selection -


i have 2 radio button name='bb' , have drop down button name='size' should have 2 different option depends on checked radio button.

code below  ----------------------------------------  <input type="radio" name="bb" id="bb" checked required="true" value="home" >  <div>home</div>  <input type="radio" name="bb" id="bb" value="office">  <div>office</div>    <label>size</label>  <select name="bedroom" id="bedroom" style="color: #8e9092; /*height: 38px;width: 228px;*/" >    home---  <option value="1 bedroom (350 700 sq ft)">1 bedroom (350 700 sq ft)</option>  <option value="2 bedroom (700 1300 sq ft)">2 bedroom (700 1300 sq ft)</option>  <option value="3 bedroom (1300 2000 sq ft)">3 bedroom (1300 2000 sq ft)</option>  <option value="4 bedroom (2000 3000+ sq ft)">4 bedroom (2000 3000+ sq ft)</option>    office--  <option value="">choose size</option>  <option value="studio (400-600 sq ft)">studio (400-600 sq ft)</option>  <option value="studio alcove (600-700 sq ft)">studio alcove (600-700 sq ft)</option>

try it: include jquery library first.

demo

html:

  <input type="radio" id="home" name="chk" value="1" checked>home <br/>   <input type="radio" id="office" name="chk" value="2" >office<br/> <br/>   <select id="describe">      <option name="name" value="1">select above</option>   </select> 

jquery:

var lista = [{name:'1 bedroom 350 700 sq ft', value:'1 bedroom 350 700 sq ft'}, {name:'2 bedroom 700 1300 sq ft', value:'2 bedroom 700 1300 sq ft'}, {name:'3 bedroom 1300 2000 sq ft', value:'3 bedroom 1300 2000 sq ft'}];  var listb = [{name:'studio 400-600 sq ft', value:'studio 400-600 sq ft'}, {name:'studio alcove 600-700 sq ft', value:'studio alcove 600-700 sq ft'}];  $(document).ready( function() {   $("input[name='chk']").on('change',function() {        if($(this).is(':checked') && $(this).val() == '1')       {         $('#describe').empty()         $.each(lista, function(index, value) {          $('#describe').append('<option value="'+value.value+'">'+value.name+'</option>');         });                         }       else if($(this).is(':checked') && $(this).val() == '2')       {         $('#describe').empty()         $.each(listb, function(index, value) {          $('#describe').append('<option value="'+value.value+'">'+value.name+'</option>');         });        }       else       {        }    }); });   

Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -