Inside your function, compare the jquery index with you, with the total number of elements you have.
I do not know your html, but I believe that you can do it.
Modify your function so that it receives the index parameter that the jquery sends.
$("option", that).each(function(index) {
Then, at the end of this function, compare the length with the index, if they match, then you are done
if ( $('option', that).length == (index +1 ) ) {
$(that).after(hiddeninput);
$(that).after(dropdowndiv);
$(that).remove();
}
, . , "" .
,