javascript - Dynamically append multiple options selected into one select jquery -
i'm trying add options selected 1 select element another. i'm guessing it'll this.
var selected = $("#selectwithoptions").append("<option'>" + options[selectedindex].value + "</option>");
this not work.
the logic pretty straight-forward , can follow inline comments in js function:
// fire function when dropdown item selected $('#dd1').change(function() { // grab text of selected item var selectedoption = $('#dd1 :selected').text(); // if not in second dropdown list, append if( $('#dd2 option').filter(function () { return $(this).text() == selectedoption; }).length <= 0 ) { $('#dd2').append('<option>'+selectedoption); } });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div>dropdown 1: <select id="dd1"> <option selected disabled>select option</option> <option>foo</option> <option>bar</option> <option>baz</option> <option>xiyar</option> </select> </div> <div>dropdown 2: <select id="dd2"> </select> </div>
Comments
Post a Comment