Get multi-class elements in JavaScript

As any person who uses JavaScript for client side interaction knows, getting elements by their class is one essential thing we need to do over and over again. For the most part using a function equivalent to the one shown below will do.

Code Snippit 1: Simplistic getElementsByClass() JavaScript method

function getElementsByClass( className ) {
    var all =
        document.all ? document.all : document.getElementsByTagName( '*' );
    var elements = new Array();
    for( var e = 0; e < all.length; e++ ) {
        if (all[e].className == className) {
            elements[elements.length] = all[e];
        }
    }
    return elements;
}

This function basically checks the string that is entered into the class attribute for each HTML element and returns all elements where there is an exact match.

This method will fail however if we're trying to select elements that have multiple class associations. We need to use something more complex when, for example, creating a list of items that are filterable by class when arranged in overlapping groups. Splitting the class attribute on space characters will allow us to check for matches in these cases:

Code Snippit 2: More advanced getElementsByClass() function

function getElementsByClass( className ) {
    var all =
        document.all ? document.all : document.getElementsByTagName( '*' );
    var elements = new Array();
    for( var e = 0; e < all.length; e++ ) {
        var classes = all[e].className.split(/\s/g);
        for( var c = 0; c < classes.length; c++ ) {
            if( classes[c] == className ) {
                elements[elements.length] = all[e];
            }
        }
    }
    return elements;
}

The function above does pretty much the same as the first one, however splits the class attribute string on the spaces and loops through them, trying to match the parts individually. This should match on elements with singular and multiple class names associated with them and solve any problems with multiple classed elements.
- Chris

Suse Tips

I’ve been using Suse at work now for over a year. Once in a while I’ll find a shortcut or a handy little thing that I’ll find useful. This blog post outlines a few of the handy shortcuts and tips that I’ve picked up and never readily knew.

Read the rest of this entry »

Duckworthing

Well it’s finally happened, the pair of glasses I’ve had for going on four or five years now has finally given up.

This morning when I arrived at work and sat at my desk I did what I usually do. I took my glasses off and gave them a clean on my shirt. Usually I just put them back on and get to the nitty-gritty, usually sitting sifting through emails to find out if any of the spam is actually useful.

Today, however was a different story indeed, as soon as I placed the glasses on my face I felt a ping and then everything went blurry. I thought that the screw holding the lens into my frames had pinged out again, yes it’s happened before, but alas no! No, the actual frame had broken right at the point where the rim meets the legs, leaving a jagged edge and a confounded Chris.

Read the rest of this entry »

Adding non-Apple format movies to iTunes

I’ve been having a bit of trouble adding some movies to my iTunes library this evening – in that I’d have to convert a movie to a .mov file in order to import it at all.

A few google searches away and I’d found the answer to all my problems. If you have the Apple development tools installed (and are obviously on a Mac), you can simply change the file meta-data of the movies via the terminal in order to allow you to import them. It’s very simple to do:
SetFile -t "MooV" movie.avi

Via: AVI to iTunes on MacRumors Forums

I did, however, have to change the iTunes settings so that it didn’t take it’s own copy of the files when importing them. I have a separate Movies folder for a reason thank-you-very-much iTunes.
- Chris

R.I.P. PB

As per my previous post, I’m getting a new machine. This is due however, to some unfortunate news: my PowerBook is dead!

Read the rest of this entry »