Archive

Posts Tagged ‘JQuery JavaScript’

JQuery for moving list option up & down

November 17, 2011 Leave a comment


// 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: