javascript - Iterate through select options with button click -


this seems should incredibly easy it's in morning , i'm drawing blank.

i have select contains % values (for zooming) , having dropdown want have 2 buttons (+ , -) iterate through list.

so assuming had:

<button id="minusbutton">-</button> <select id="zoomselect" onchange="zoom()">       <option value="10">10%</option>       <option value="20">20%</option>       <option value="50">50%</option>       <option value="100" selected="selected">100%</option> </select> <button id="plusbutton">+</button> 

how go switching , down select each time button pressed. ensuring stops nicely on 100% , 10% (ie, no wrapping round or throwing error if keep pressing +).

thanks much!

$(document).on('click', '#minusbutton', function() {    var selindex = $("#zoomselect").prop('selectedindex');    if (selindex != 0) {      $("#zoomselect").val($('#zoomselect option').eq(selindex - 1).val());      zoom();    }  });  $(document).on('click', '#plusbutton', function() {    var selindex = $("#zoomselect").prop('selectedindex');    if (selindex != $('#zoomselect option').length - 1) {      $("#zoomselect").val($('#zoomselect option').eq(selindex + 1).val());      zoom();    }  });    function zoom() {    console.log('zoomed');  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <button id="minusbutton">-</button>  <select id="zoomselect" onchange="zoom()">        <option value="10">10%</option>        <option value="20">20%</option>        <option value="50">50%</option>        <option value="100" selected="selected">100%</option>  </select>  <button id="plusbutton">+</button>

click event can written such change value of select box, , trigger zoom function accordingly.


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -