DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

FirstLast (real Classes For Ajaxability)

  • submit to reddit
        I know CSS has the :first-child and :last-child pseudo classes.  But :first-child is a part of CSS 2 and has poor browsers support and :last-child is a part of CSS 3 and that's not worth thinking about using yet.  The idea is sound though, so I worked up this JavaScript method of getting the same effect.

One advantage of doing this in JavaScript rather than using CSS is that the classes will change if you reorder the child nodes with more JavaScript.  In my case I'm using <a href="">Scriptaculous sortables</a>.  Or to be more specific, I'm using a <a href="">Ruby on Rails</a> helper method to make something sortable through Scriptaculous:
<%= sortable_element 'image-list',
    :constraint => false,
    :url => { :action => 'sort', :issue_id => params[:issue_id] }

I've chosen to not make this script run unobtrusively.  Most of it can go into a file and be included in the header or added to your own common JavaScript include file.  To get it to run on a page you will need to include something like the below in each page.
<script type="text/javascript">
        onComplete: function(){FirstLast.go("image-list");}
I use the <a href="">DOMReady extension</a> for Prototype's Event object, but you can use any loader you want.  The Ajax.Responders.register part reruns the script after a drag-and-drop operation (or any Ajax operation really).

<a href="">Download Scriptaculous and Prototype.</a>

var FirstLast = {
    go: function(el) {
        el = $(el);

        // Whitespace nodes need to be cleaned to get the intended effect
        var children = Element.cleanWhitespace(el).childNodes;

        // Return if there are not any children
        if (0 == children.length) return
        if (1 == children.length) {
            // Cheap shortcut if there is only 1 child node
        } else {
            for (var i = 0; i < children.length; i++) {
                switch (i) {
                    // First child
                    case 0:
                    // Last child
                    case children.length - 1:
                    // Every child other than the first or last
                        break;  // I know it is unnecessary
    // Pseudo Private methods and attributes
    _firstChildClassName: "first",
    _lastChildClassName: "last"