Xyzk
Xyzk

Reputation: 1332

moving last child to first position does not work jquery

I have tried moving last element in my list to first position on click, but it does not work. I did it according to answer from this question: Move last child to first position . Firebug shows me that something happens with the list, but it's certainly not last to first change :(

jQuery:

$(document).ready(function(){   
    $(".carousel-button-left").click(function(){
    var $ul=$(this).next();                 
    var $lastchild=$ul.children().last();
    $ul.prepend($lastchild);
    });
});

HTML:

<div class="testowyDiv">
    <img class="carousel-button-left" src="ikony/strzalkal.png">
    <div class="testowyDiv2">
        <ul class="testowyUl">
            <li>1</li>
            <li>2</li>
            <li>3</li>
            <li>4</li>
            <li>5</li>
            <li>6</li>
            <li>7</li>
            <li>8</li>
            <li>9</li>
        </ul>
    </div>
    <img class="carousel-button-right" src="ikony/strzalkap.png">
</div>

Upvotes: 0

Views: 2119

Answers (3)

metalfight - user868766
metalfight - user868766

Reputation: 2750

$(document).ready(function(){   
    $(".carousel-button-left").click(function(){
    var $ul=$(".testowyUl");  //do you need node traversing, if not use direct, better keep a id for it 
    var lastchild=$ul.children().last();
        console.log(lastchild.val());
    $ul.prepend(lastchild);
    });
});

Demo -Fiddle

Upvotes: 0

long.luc
long.luc

Reputation: 1191

Using prepend or insertBefore.

Sample:

$(document).ready(function(){   
    $('.carousel-button-left').click(function() {
        var first = $('.testowyUl li:first');
        var last = $('.testowyUl li:last');
        last.insertBefore(first);
    });
});

Upvotes: -1

Rituraj ratan
Rituraj ratan

Reputation: 10378

$(document).ready(function(){   
    $(".carousel-button-left").click(function(){
    var $ul=$(this).next().find(".testowyUl");   // by next we reach on next div then by find we reach on ul               
    var $lastchild=$ul.children().last();
    $ul.prepend($lastchild);
    });
});

Upvotes: 4

Related Questions