Skip to content Skip to sidebar Skip to footer

Create New Drop Downs On Change Event Using Jquery

I have drop down menu with some random values. When I select the value onchange event triggers and I want to add new drop down under it, but the new one should have all values exce

Solution 1:

We can do it in following way:

We are just adding some class like executed once it add another dropdown. And in event handler we are checking if current dropdwon has that class or not. If it is already there then it means we are already done with that dropdown as follows:

$(document).ready(function() {
  var selectWrapper = $('#select-boxes');
  $(document).on('change', '.dynamic-select', function() {
    var element = $(this);
    if(element.hasClass("executed"))
      return;
    
    var optionsLength = (element.find('option').length) - 1;

    if (optionsLength === 1) {
      returntrue;
    }

    var newSelect = $(this).clone();
    newSelect.find("option[value='" + element.val() + "']").remove();
    newSelect.appendTo(selectWrapper)
    
    $(this).addClass("executed");
    
  });
});
.dynamic-select {
  display: block;
  margin: 10px;
}
<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script><divid="select-boxes"><selectclass="dynamic-select"id="ddlTest"><optionvalue=""></option><optionvalue="Belbin">Belbin</option><optionvalue="Raven">Raven</option><optionvalue="PPA">PPA</option><optionvalue="PPA+">PPA+</option><optionvalue="Basic Knowledge">Basic Knowledge</option><optionvalue="PCT">PCT</option></select></div>

Post a Comment for "Create New Drop Downs On Change Event Using Jquery"