0

how can I move element to end but keep the variable updated without traversing again?

var pbUL = $('ul');
var pbLIs = $('li');
pbLIs.eq(0).appendTo(pbUL); 
pbLIs = $('li'); // not good...

simple problem - probably simple soluction, but I have no Idea how to solve...

Ron
  • 3,975
  • 17
  • 80
  • 130
  • I don't get it, you allready have the matched set of elements, are you expecting it to change because you move it in the DOM? – adeneo Feb 12 '13 at 19:04
  • I don't understand what you want to achieve. The last line is useless. Prior to it, pbLIs' reference is still the same jQuery object, it never changed. Please provide an example of what you want to achieve. – glmxndr Feb 12 '13 at 19:05
  • What browser and version of jQuery are you using? [It's working as intended here](http://jsfiddle.net/mblase75/sLPEz/). – Blazemonger Feb 12 '13 at 19:05
  • I guess we need to know what you are planning on doing with the li elements after appending the 1st one? – Lee Meador Feb 12 '13 at 19:08
  • @subtenante It's the same jQuery object, never changed, meaning it's still in the original sort order, not the updated sort order. – Kevin B Feb 12 '13 at 19:11
  • I don't get why this was downvoted. It's a legit question. – AlienWebguy Feb 12 '13 at 21:40

2 Answers2

1

Why not this:

(function( $ ) {
    $.fn.shift = function() {
        var bottom = this.get(0);
        this.splice(0,1);
        return bottom;
    };
})( jQuery );

var arr = $('li');
arr.push(arr.shift());

Demo: http://jsfiddle.net/m6Pf6/

AlienWebguy
  • 76,997
  • 17
  • 122
  • 145
0

Try replacing this part:

pbLIs.eq(0).appendTo(pbUL); 
pbLIs = $('li'); // not good...

with this:

pbLIs = pbLIs.eq(0).appendTo(pbUL); 
Lee Meador
  • 12,829
  • 2
  • 36
  • 42
  • This returns the `li` that was appended, not all `li`'s – Kevin B Feb 12 '13 at 19:06
  • He already has a list of all the matching elements. The one that moved is still there even though it moved locations. I thought OP wanted the one that moved so he/she could do something with it. (I guess that doesn't make sense given the OP's last line of code.) – Lee Meador Feb 12 '13 at 19:10