Is this .recur
an <option>
element within a <select>
?
Like this?
<select name="cars" id="cars">
<option class="recur" value="volvo">Volvo</option>
<option class="recur" value="saab">Saab</option>
<option class="recur" value="mercedes">Mercedes</option>
<option class="recur" value="audi">Audi</option>
</select>
Why do you need to change the index? are you attempting to re-order items in this dropdown list?
Your code is currently setting an index
attribute to 0 for every element. I assume that's not what you're trying to achieve
I'd recommenced making a function for "reordering" them if that's what you intend to do. Rather than doing a multiple things at once in one function.
If you are trying to sort this alphabetically, another stack overflow answer linked here has you covered with the use of jQuery
https://stackoverflow.com/a/667198/20053031
const SortDropdownList = () => {
$("#cars").html($("option").sort(function (a, b) {
return a.text == b.text ? 0 : a.text < b.text ? -1 : 1
}))
}
If you are intending to set the first element as selected, just do the following:
$('#cars option').first().attr('selected', true);