sans-serif   serif

«

JQuery 1.4’s new ‘until’ methods caught my eye … introducing Element.GetUntil

January 15, 2010

So I did what every good mootools developer would … extended MooTools to do the same. The names in jQuery are nextUntil, prevUntil, and parentsUntil. I decided to stick to mootools tradition and make my code more clear: getAllNextUntil, getAllPreviousUntil, and getParentsUntil.

Download it from the forge, fork it on github.

Luckily, this doesn’t ship with every copy of MooTools core, I can’t imagine very many practical uses–but like I said, it caught my eye, here’s a demo.

Here’s the source if you’re interested:

(function(){

    var walkUntil = function(element, walk, match, nocash){
        var el = element[walk];
        var elements = [];
        while (el){
            if (el.nodeType == 1){
                if (!match || Element.match(el, match)) {
                    break;
                } else {
                    elements.push(el);
                }
            }
            el = el[walk];
        }
        return new Elements(elements, {ddup: false, cash: !nocash});
    }

    Element.implement({

        getAllPreviousUntil: function(match, nocash){
            return walkUntil(this, 'previousSibling', match, nocash);
        },

        getAllNextUntil: function(match, nocash){
            return walkUntil(this, 'nextSibling', match, nocash);
        },

        getParentsUntil: function(match, nocash){
            return walkUntil(this, 'parentNode', match, nocash);
        }

    });

})();

MooTools in this article

Related Posts:

Comments

  • http://meiocodigo.com Fábio M. Costa

    Very nice Ryan! I love your blog and contributions!

  • http://pjhh.wordpress.com/2010/01/18/making-use-of-element-getuntil/ Making use of Element.getUntil « Personal Journal of Henrik Hansen

    [...] 18, 2010 Ryan Florence made a blog post two days ago about implementing jQuery’s getUntil in MooTools. He did a fantastic job, but hwe concluded the post saying that saying that is not many real uses [...]

  • Henrik Hansen

    Hey Ryan, I’ve actually used your code to something (semi) serious. I’ve used it to create a range selection: http://pjhh.wordpress.com/2010/01/18/making-use-of-element-getuntil/

  • http://dev.enekoalonso.com Eneko Alonso

    Nice… very nice :)

  • http://ryanflorence.com/jquery-1-4-mootools-1-2-compared/ Jquery 1.4, MooTools 1.2 Compared « Ryan Florence Blog, MooTools Tutorials and More

    [...] these new until methods were interesting so I extended MooTools with similar functionality. See my post about [...]

blog comments powered by Disqus

Comments RSS