How to build custom form controls - need help with enldess list cycling

Hi there!
I’ll appreciate any help on this small task posed here

(please find on this page the line “We’ll leave adding this missing functionality as a reader exercise.” just to get an idea what I’m asking for.
Here’s my version for possible solution
However it only works in Firefox other browsers don’t do it the supposed way.

So, the question is how one would make prevent endless cycling through the list of options.

Thank you.