Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

moving down selected option is not visible

Tags:

html

jquery

css

when i move the first option to the bottom, at some point i cannot see the option in the select list, can anyone help ?

$(document).ready(function() {
  $('input[type="button"]').click(function() {
    var $op = $('#select2 option:selected'),
      $this = $(this);
    if ($op.length) {
      ($this.val() == 'Up') ?
      $op.first().prev().before($op):
        $op.last().next().after($op);
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select multiple id="select2">
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
  <option value="12">12</option>
  <option value="23">23</option>
  <option value="34">34</option>
  <option value="11">11</option>
  <option value="25">25</option>
  <option value="83">83</option>
  <option value="81">81</option>
  <option value="82">82</option>
  <option value="73">73</option>
</select>
<input type="button" value="Up">
<input type="button" value="Down">
like image 365
Manu S Avatar asked Nov 09 '22 22:11

Manu S


1 Answers

Here is a solution with scrolling:

$('input[type="button"]').click(function() {
  var $op = $('#select2 option:selected'),
    $this = $(this);
  if ($op.length) {
    ($this.val() == 'Up') ?
    $op.first().prev().before($op):
      $op.last().next().after($op);
  }

  var $s = $('#select2');
  $s.scrollTop($s.scrollTop() + ($op.offset().top - $s.offset().top));

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select multiple id="select2">
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
  <option value="12">12</option>
  <option value="23">23</option>
  <option value="34">34</option>
  <option value="11">11</option>
  <option value="25">25</option>
  <option value="83">83</option>
  <option value="81">81</option>
  <option value="82">82</option>
  <option value="73">73</option>
</select>
<input type="button" value="Up">
<input type="button" value="Down">

Note that you can still finetune it, and only make scroll when its needed, and have it at bottom (instead of top) when you scroll down

like image 93
CoderPi Avatar answered Nov 14 '22 23:11

CoderPi