Search code examples
javascripthtmlmootools

mootools clone() inject('top') reverses the element order order


Using clone(rue, false).inject('myList', 'top') reverses the item order when 'top'is the where string. There must be a elegant way of sorting this? is my approach wrong?

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-gb" xml:lang="en-gb">
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <script type="text/javascript" src="js/moo.1.4.5/core.js"></script>
    <script type="text/javascript">
        window.addEvent('domready', function () {


            $('myList').getChildren().each( function (el, i)  { 
                el.clone(true, false).addClass('clone').inject('myList', 'top');
                el.clone(true, false).addClass('clone').inject('myList', 'bottom');
            }) ;


        });
    </script>
</head>
<body>
    <ul id="myList">
        <li><p>Slide 1</p></li>
        <li><p>Slide 2</p></li>
        <li><p>Slide 3</p></li>
        <li><p>Slide 4</p></li>
        <li><p>Slide 5</p></li>
    </ul>
</body>
</html>

returns

<ul id="myList">
   <li class="clone"><p>Slide 3</p></li>
    <li class="clone"><p>Slide 2</p></li>
    <li class="clone"><p>Slide 1</p></li>
    <li><p>Slide 1</p></li>
    <li><p>Slide 2</p></li>
    <li><p>Slide 3</p></li>
    <li class="clone"><p>Slide 1</p></li>
    <li class="clone"><p>Slide 2</p></li>
    <li class="clone"><p>Slide 3</p></li>
</ul>

Solution

  • depends on your objective. if your aim is to clone the list and inject it at the top, you can do this along your current line of thinking:

    http://jsfiddle.net/dimitar/xNWrM/

    var list = $('myList'),
        lis = list.getElements('li');
    
    lis.reverse().each(function(el){
        el.clone(true, true).addClass('clone').inject(list, 'top');
    });
    

    there may be a more elegant way to clone them by injecting after the previously injected element as you loop them, this will save the Elements.reverse call.