3
<select id="form">
    <option value="1" >1 </option>
    <option value="2" >2 </option>
    <option value="3" >3 </option>
</select>
<button id='next'>Next</button>
<script>
    var select2 = document.getElementById("form");
    var lastselected2 = localStorage.getItem('select2');
    document.getElementById('next')
            .addEventListener('click', function(){
                  lastselected2 = parseInt(lastselected2) + 1;
            });

    if(lastselected2) {
        select2.value = lastselected2; 
    }

    select2.onchange = function () {
        lastselected2 = select2.options[select2.selectedIndex].value;
        console.log(lastselected2);
        localStorage.setItem('select2', lastselected2);
    }
</script>

I've this code that saves the selected of the dropdown and I wanted to add a button that can select the next option of the dropdown.

Denzadeud
  • 63
  • 1
  • 5

2 Answers2

3
<script>
function selectNext(){
  var select = document.getElementById('form');
  select.selectedIndex++;
}
</script>

<button id='next' onclick="selectNext()">Next</button>

This is a good option to find the actual index selected and go direct to the next one, by index.

https://developer.mozilla.org/en-US/docs/Web/API/HTMLSelectElement/selectedIndex

zelda11
  • 425
  • 2
  • 7
0

Try this?

var button = document.getElementById('next');
button.onclick = function() {
   select2.value = parseInt(lastselected2)+1;
}

Quick and dirty I know...

Dan Clarke
  • 246
  • 2
  • 11