I am trying to randomise part of the order of options in a select dropdown. Keeping the first option first and latter two options last.
<select class="form-control" id="return-reason">
<option value="" disabled="disabled" selected="selected">Please select...</option>
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
<option>Option 4</option>
<option>Option 5</option>
<option>Option 6</option>
<option>Prefer not to say</option>
<option>Other</option>
</select>
I am working from the idea of randomising the whole list:
var ul = document.querySelector('#return-reason');
for (var i = ul.children.length; i >= 0; i--) {
ul.appendChild(ul.children[Math.random() * i | 0]);
}
I can place the 'please select' option at the bottom of the HTML list, and by running a slightly amended loop, it will append a randomised list (less the 'please select' option) after 'please select'.
var ul = document.querySelector('#return-reason');
for (var i = ul.children.length + 1; i >= 0; i--) {
ul.appendChild(ul.children[Math.random() * i | 0]);
}
But this still doesn't take care of keeping the 'prefer not to say' and 'other' options at the bottom. I'm not entirely sure that I can achieve my requirements this way, and it doesn't feel right.
Can anyone kindly advise a way forward.
For my own purposes it doesn't have to be 100% accurately random.