I'm currently removing duplicates from a dropdown menu using the following script, but I now need to try to remove all duplicates apart from the LAST option to enable the search criteria to be remembered by Solspace's Freeform.
Does anyone cleverer than me know how to adjust the script to remove all duplicates APART from the last one?
// REMOVE DUPLICATES FROM LOCATION DROPDOWN
var optionValues =[];
$('#locationList option').each(function(){
if($.inArray(this.value, optionValues) >-1){
$(this).remove()
}else{
optionValues.push(this.value);
}
});
Thanks in advance,
Tom
$(document).ready(function() {
var optionValues = [];
var lastRemoved = null;
$('#locationList option').each(function(){
if($.inArray(this.value, optionValues) >-1){
$(this).remove();
// remember the very last removed one
lastRemoved = $(this);
}else{
optionValues.push(this.value);
}
});
// after removing duplicates, add the very last removed one back to the list
$('#locationList').append(lastRemoved);
});
Assuming I understood your problem correctly, this will remove all the duplicates from the list excluding the very last occurrence. Let me know if that helps!