javascript - Dynamically append dropdown's value text to another dropdown -


<select id="mg_sport_game_id1">    <option value="">select</option>    <option value="25">titans</option>    <option value="35">batman</option> </select> 

now have fetch above dropdown's value , append value,text other dropdown

<select id="schedule_winner">    <option value="">select</option> </select> 

i trying follows

var team1=$("#mg_sport_game_id1").val(); var myoptions = {     guest_name : guest,     ($("#mg_sport_game_id1").options[$("#mg_sport_game_id1").selectedindex].text) : team1 };  $.each(myoptions, function(val, text) {     $('#schedule_winner').append( new option(text,val) ); }); 

i ending following error :

syntaxerror: invalid property id

i don't know trying achive, maybe can you:

$('#mg_sport_game_id1').on('change', function() {    var val = $(this).val(),     text = $(this).find("option:selected").text();    $('#schedule_winner').append(new option(text, val));  }); 

what here:

  • register event handler change event
  • get value , text selected option.
  • append new option other select

example


Comments