Home > Uncategorized > JQuery for moving list option up & down

JQuery for moving list option up & down


// moves the selected option upwards in the list with the given id
function moveUp(listId) {
jQuery('select[id$="' + listId + '"]').children('option').each(function(index) {
if (jQuery(this).attr('selected')) {
var prev = jQuery(this).prev().get(0);
if (prev == null) {
return;
}
this.parentNode.insertBefore(this, prev);
}
});
}
// moves the selected option downwards in the list with the given id
function moveDown(listId) {
jQuery.fn.reverse = [].reverse;
jQuery('select[id$="' + listId + '"]').children('option').reverse().each(function(index) {
if (jQuery(this).attr('selected')) {
var next = jQuery(this).next().get(0);
if (next == null) {
return;
}
this.parentNode.insertBefore(next, this);
}
});
}

Categories: Uncategorized Tags:
  1. No comments yet.
  1. No trackbacks yet.

Leave a comment