Friday, June 11, 2010

Use jQuery to sort page elements

I was using jQuery to create some span tags dynamically based on user input, and I wanted those tags to be sorted alphabetically by their id's. The span tags are children of a table cell.

Basically, use jQuery to get the parent container and a list of the things you want to sort. Then, call sort() with an anonymous function that sorts how you want. Finally, iterate the list, appending the items to the parent container. When you append something that already exists, it just moves the item in the DOM, which is exactly what we want. I can call this code each time I add a new span tag, and the list stays sorted...nice.
// sort span tags within a td

var $td = $('#tdMyItems');
var itms = $td.children('span').get();

itms.sort(function (a, b)
{
    var compA = $(a)[0].id;
    var compB = $(b)[0].id;
    return (compA > compB) ? -1 : (compA < compB) ? 1 : 0;
})

$.each(itms, function (idx, itm)
{
    $td.append(itm);
});


Wednesday, June 2, 2010

Google Chrome App Mode

Ok, now this is cool. You can start Google Chrome in "App Mode" which gets rid of all of the controls, address bar, extensions, etc.

For instance, you could start facebook like this:

chrome --app="http://www.facebook.com"

I like it. I run Meebo that way too.