» Programming » jQuery » swapUp and swapDown

These are the functions used in the beta version of the “Repost.Me Social Link Icon Widget” to enable easy re-arranging of items within a list.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
jQuery.fn.swapUp = function() {
  var a = this[0];
  var p = this.prev();
  if (p.length < 1) return this;
  var b = p[0];
  var t = a.parentNode.insertBefore(document.createTextNode(''), a);
  var x = this.css('background');
  b.parentNode.insertBefore(a, b);
  t.parentNode.insertBefore(b, t);
  t.parentNode.removeChild(t);
  this.css('background', p.css('background'));
  p.css('background', x);
  return this;
}
 
jQuery.fn.swapDown = function() {
  var a = this[0];
  var p = this.next();
  if (p.length < 1) return this;
  var b = p[0];
  var t = a.parentNode.insertBefore(document.createTextNode(''), a);
  var x = this.css('background');
  b.parentNode.insertBefore(a, b);
  t.parentNode.insertBefore(b, t);
  t.parentNode.removeChild(t);
  this.css('background', p.css('background'));
  p.css('background', x);
  return this;
}