View Single Post
Old 01-19-2013, 10:46 PM   PM User | #12
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
Quote:
Could that mean the elements haven't loaded yet?
Yes, as has been mentioned a couple of times . You need to run the code that attaches events (using getElementById) after the page has loaded. You could either move it into code that runs on the window-load event or move all your JS to the bottom of the page, just before the closing BODY tag.

You might use a slightly extended version of your dollar-function:

Code:
        var $ = function () {
            // Example: var els = $('obj1name',obj2,'obj2name');
            // Saves having to write 'document.getElementById' repeatedly,
            // but could also be useful for grouping elements.
            var elements = {}, argLen, i, element; 
            for (i = 0, argLen = arguments.length; i < argLen; i++) {
                element = arguments[i];
                if (typeof element === 'string')
                    element = document.getElementById(element);
                if (argLen === 1)
                    return element;
                elements.push(element);
            }
            return elements;
        };
It is no where near as comprehensive as jQuery's but slightly more flexible than your current version. You can supply it a string (an elements' id), an element-object reference, or several of these separated by commas.
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
AndrewGSW is offline   Reply With Quote