If you understand correctly, you are trying to add a new select to your div at the click of a button. If so, an easy way would be to restructure your html to have a .add-more div before your actual (or initial selection window).
<ul> <li> <div class="add-more"></div> <div class="worker-container-last"> <label class="worker-label"> <select name="title[]" class="title"> <option value="Select a Title" selected="selected">Select a Title</option> <option value="Boss">Boss</option> <option value="Worker">Worker</option> <option value="Manager">Manager</option> </select> </label> </div> <div><a class="worker" title="" href="">Add Another Worker</a></div> </li> </ul>
And in your js use prepend instead of append something like:
$(document).ready(function(){ $('.worker').on('click', function(e){ e.preventDefault(); e.stopPropagation(); var title = $('.title').val(); var worker = '<li><select name="title[]" class="title"><option value="Select a Title" selected="selected">Select a Title</option><option value="Boss">Boss</option><option value="Worker">Worker</option><option value="Manager">Manager</option></select></li>'; $('.add-more').prepend(worker);
Working fiddle: https://jsfiddle.net/nje3opu7/
EDIT , if you do not want to change HTML or CSS , as you said in your comments, you can change the jQuery logic as follows:
$(document).ready(function(){ $('.worker').on('click', function(e){ e.preventDefault(); e.stopPropagation(); var getValue = $('.title:first').val() var worker = '<select name="title[]" class=" newSelect title"><option value="Select a Title" selected="selected">Select a Title</option><option value="Boss">Boss</option><option value="Worker">Worker</option><option value="Manager">Manager</option></select>'; $('.add-more').append(worker); $('.title:eq('+$('.newSelect').length+')').val(getValue); $('.title:first').val('Select a Title'); }); });
Working fiddle: https://jsfiddle.net/97tnnyj0/
Edit2: Even @KAD code will work fine if you replace:
jqueryselect.val($('.title:last').val()); with jqueryselect.val($('.title:first').val());
source share