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">
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With