Programmer, game designer, geek.
Catching Elephant is a theme by Andy Taylor
So I decided to pick up JS and give it a bit of a better whirl than I did last time I used it. It feels like a real guilty pleasure to me, because it allows you to do the most hacky things imaginable - heck, even encourages it. It’s really fun and versatile and powerful but it just makes me feel so dirty…
For example: I had a function that had an element passed in and I was using element.childNodes to get all the sub-elements of that element and perform some magic on them - so far so good. But then my girlfriend, in tidying up the markup a little, decided to drop those child nodes down one level (essentially) by putting them in a group of divs. Panic! To begin with, I thought “well, what about looping through each div and compiling an array of all the children - ie the nodes I actually want?” but that sounded horribly complex and I thought “there must be a simpler way of doing this”. I knew you could do “document.getElementsByTagName()” and I thought “hey, maybe you can do that to any element”. Turns out you can! And I modified the function to change references to element.childNodes to become element.getElementsByTagName(). Javascript even allows you to take a shortcut to the returned array by adding [] after the () in the function call, meaning it was literally just a case of find-and-replace “childNodes” for “getElementsByTagName(‘input’)”. Pretty hacky if you ask me, but it works. YMMV on other browsers, but this is an application that’s only for us so it doesn’t need to run cross-browser.