Am trying to change the dropdown value based on prev and next buttons But if the option is the last in dropdown am unable to go to first one and if the option is first am unable to go to last one. Can anyone help in this.Here is fiddle code..
$("#next").click(function() {
var nextElement = $('#selectBox > option:selected').next('option');
if (nextElement.length > 0) {
$('#selectBox > option:selected').removeAttr('selected').next('option').attr('selected', 'selected');
}
});
$("#prev").click(function() {
var nextElement = $('#selectBox > option:selected').prev('option');
if (nextElement.length > 0) {
$('#selectBox > option:selected').removeAttr('selected').prev('option').attr('selected', 'selected');
}
});
function currentSlide(selectionValue) {
console.log(selectionValue);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button type="button" id="prev">Previous</button>
<select id="selectBox" onchange="currentSlide(value);" class="selectpicker">
<option value="1" class="options">Electronics</option>
<option value="2" class="options">Clothing</option>
<option value="3" class="options">Health</option>
<option value="4" class="options">Food</option>
<option value="5" class="options">Travel</option>
<option value="6" class="options">Mobiles</option>
<option value="7" class="options">Grocery</option>
<option value="8" class="options">Recharge</option>
<option value="9" class="options">Furniture</option>
</select>
<button type="button" id="next">Next</button>