0

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>
sabithpocker
  • 15,274
  • 1
  • 42
  • 75

5 Answers5

2

Try using this solution for loopNext Try to write your code for loopPrev by editing the same!

Use prop to set property instead of attr to change selected value in dropdown.

$.fn.loopNext = function(selector) {
  var selector = selector || '';
  return this.next(selector).length ? this.next(selector) : this.siblings(selector).addBack(selector).first();
}
$.fn.loopPrev = function(selector) {
  var selector = selector || '';
  return this.prev(selector).length ? this.prev(selector) : this.siblings(selector).addBack(selector).last();
}
$("#next").click(function() {
  $('#selectBox > option:selected')
    .removeAttr('selected')
    .loopNext('option')
        .prop('selected', 'selected');
});

$("#prev").click(function() {
  $('#selectBox > option:selected')
    .removeAttr('selected')
    .loopPrev('option')
        .prop('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>
Community
  • 1
  • 1
sabithpocker
  • 15,274
  • 1
  • 42
  • 75
2

Try this. Actually when nextElement.length = 0, you know that it's already the first/last option, so you will only need to select the corresponding last/first option

var total_options = $('#selectBox option').length;

$("#next").click(function() {
  var curr_op = $('#selectBox').val();
  if (++curr_op > total_options) curr_op = 1;
  $('#selectBox').val(curr_op);
});

$("#prev").click(function() {
  var curr_op = $('#selectBox').val();
  if (--curr_op < 1) curr_op = total_options;
  $('#selectBox').val(curr_op);
});

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>
Hoang Do
  • 325
  • 1
  • 14
1

Try this :

$(function() {
  $("#next").click(function() {
    var nextElement = $('#selectBox > option:selected').next('option');
    if (nextElement.length > 0) {
      nextElement.prop('selected', 'selected');
    } else {
      $('#selectBox > option:eq(0)').prop('selected', 'selected');

    }
  });

  $("#prev").click(function() {
    var nextElement = $('#selectBox > option:selected').prev('option');
    if (nextElement.length > 0) {
      nextElement.prop('selected', 'selected');
    } else {
      var lastIndex = ($("#selectBox > option").length) - 1;

      $('#selectBox > option:eq(' + lastIndex + ')').prop('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>
R.K.Saini
  • 2,678
  • 1
  • 18
  • 25
0

Try this

$("#next").click(function() {
  var getIndex =$("select option:selected").index();
  var index=getIndex+1;
   $("select").children().removeAttr('selected');
   $("select").children().eq(index).attr('selected', 'selected');
});

Working Demo

Satinder singh
  • 10,100
  • 16
  • 60
  • 102
0

$(window).load(function() {
  $("#next").click(function() {
    var nextElement = $('#selectBox > option:selected');
    if (nextElement.length > 0) {
      $('#selectBox > option:selected').removeAttr('selected').next('option').attr('selected', 'selected');
    }
  });

  $("#prev").click(function() {
    var selectedIndex = $("#selectBox > option:selected").index();
    var nextElement = $('#selectBox > option:selected').prev('option');
    if(selectedIndex == 0){
        $('#selectBox option').last().prop('selected',true);
    }else if (nextElement.length > 0) {
 $('#selectBox > option:selected').removeAttr('selected').prev('option').attr('selected', 'selected');
    }
 });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/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>
Bhaskara Arani
  • 1,556
  • 1
  • 26
  • 44