avatar

Michaël Duerinckx

 

Posts Tagged ‘Coding’

JavaScript: getChildNodesByTagName()

If you’ve ever used JavaScript’s getElementsByTagName(), you may have noticed something annoying about its behaviour. (Well, it’s only annoying depending on your application.)

getElementsByTagName returns every element matching the tag name given, regardless of its position in the document tree. This is annoying if you only want to use the elements that are direct descendants of the element you’re calling it from.

So anyway, to cut this short, I wrote  a quick but effective function that works just the same as getElementsByTagName, with the difference that it only returns direct descendants. Just add in this snippet of code and you can start using the function just as you would use getElementsByTagName.

Object.prototype.getChildNodesByTagName = function (tagName) {
    var byTagName = this.getElementsByTagName(tagName);
    var outArr = [];
    for(a=0;a<byTagName.length;a++) {
        if(byTagName[a].parentNode == this) {
            outArr[outArr.length] = byTagName[a];
        }
    }
    return outArr;
}

Here’s how it works:
The function uses the typical getElementsByTagName, and then iterates over all the elements found. For each element it checks whether the element’s parentNode equals this, which is the node that should be the parent of any elements we want to return.
When the comparison is positive, the element is added to the array, which is later returned.

I hope you can put it to good use!

Leave me a comment if this was helpful to you, or if there’s anything wrong, please.

The Audio Portal

Well, the secret project has been out for nearly a month now. It is named “The Audio Portal” and can be found here: http://theaudioportal.com.

That’s right, I’m only now getting around to make a blog post about it. TAP, as I like to call it, has been in development since August 2009. I’ll admit, the site was inspired by Newgrounds. As if it wasn’t obvious yet. Over the 2 years I’ve been active in the Newgrounds audio community, I kept hearing about the same problems with the system, as well as experiencing them personally.

We all (we being the active community) realized that complaining about it wasn’t going to fix the problems we experienced, instead, we frowned upon forum topics regarding so-called “zerobombing”, or topics about missing genres and so on. I felt that there should be something like Newgrounds, but focussed just on music.

(more…)