Skip to content
Advertisement

The first dropdown selected and displays the same selected option value on the second dropdown

I have two select dropdowns. I am displaying the first dropdown on page load and the second dropdown is dynamically displaying.

Now what I am doing is, When the user selects anything from the first dropdown then the same option to be selected on the second dropdown after a click on the anchor tag.

I tried below code but there is some issue.

$(wrapper).append('<select name="pp_fileStatus[]" class="fileStatus"><option disabled="" selected="">Select</option><option value="1"'if(fileStatus==1){"selected";}'> One</option><option value="2"' if(fileStatus==2){"selected";}'> Two</option><option value="3"'if(fileStatus==3){"selected";}'> Three</option></select>');

$(document).ready(function() {
  $('.fileStatus').change(function() {
    var fileStatus = $('.fileStatus option:selected').val();
  })

  var wrapper = $(".appentInside .row"); //Fields wrapper
  var add_button = $(".click_me"); //Add button ID
  $(add_button).click(function(e) { //on add input button click
    e.preventDefault();

    $(wrapper).append('<select name="pp_fileStatus[]" class="fileStatus"><option disabled="" selected="">Select</option><option value="1"> One</option><option value="2">Two</option><option value="3"> Three</option></select>');

  });

});
<select name="pp_fileStatus[]" class="fileStatus">
  <option disabled="" selected="">Select</option>
  <option value="1"> One</option>
  <option value="2"> Two</option>
  <option value="3"> Three</option>
</select>

<a href="javascript:void(0);" class="click_me">click me to display seocnd dropdown</a>
<div class="appentInside">
  <div class="row"></div>
</div>


<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

Advertisement

Answer

You can do it like this by adding 2 small lines of code:

var fileStatus = $('.fileStatus:last option:selected').val(); // <-- This line 
$(wrapper).append('<select name="pp_fileStatus[]" class="fileStatus"><option disabled="" selected="">Select</option><option value="1"> One</option><option value="2">Two</option><option value="3"> Three</option></select>');
$('.fileStatus:last').val(fileStatus); // <-- This line 

var fileStatus = $('.fileStatus:last option:selected').val(); this will select the value of the last dropdown that exist.

$('.fileStatus:last').val(fileStatus); this will set the last dropdown (aka the newly created) with the previous value.

demo

$(document).ready(function() {
  $('.fileStatus:first').change(function() {
    var fileStatus = $('.fileStatus option:selected').val();
    $('.fileStatus:last').val(fileStatus);
  })

  var wrapper = $(".appentInside .row"); //Fields wrapper
  var add_button = $(".click_me"); //Add button ID
  $(add_button).click(function(e) { //on add input button click
    e.preventDefault();
    var fileStatus = $('.fileStatus:last option:selected').val();
    $(wrapper).append('<select name="pp_fileStatus[]" class="fileStatus"><option disabled="" selected="">Select</option><option value="1"> One</option><option value="2">Two</option><option value="3"> Three</option></select>');
    $('.fileStatus:last').val(fileStatus);
  });

});
<select name="pp_fileStatus[]" class="fileStatus">
  <option disabled="" selected="">Select</option>
  <option value="1"> One</option>
  <option value="2"> Two</option>
  <option value="3"> Three</option>
</select>

<a href="javascript:void(0);" class="click_me">click me to display seocnd dropdown</a>
<div class="appentInside">
  <div class="row"></div>
</div>


<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
Advertisement