If you don't understand how to do any of the following Firebug tasks, read a Firebug debugging (Michael Sync's tutorial looks good).
Switch to 'fadein.js' and set a breakpoint (that red dot) on the 'for' loop that adds the load event listener. First thing you'll notice is you can't inspect $('chalkboard') as an object and document.getElementById() is called repeatedly. This is a hint you should store $('chalkboard') in a local variable. Use a function so you don't pollute the global namespace.
Code:
(function () {
var chalkboard = $('chalkboard');
for (var i = 0; chalkboard.childNodes.length > i; i++) {
...
}
})();
The second thing you'll notice is the "if (!$("chalkboard").childNodes[i].src)" test always succeeds. When you inspect the chalkboard variable (after you add it to the code) you'll find that none of its children are <img>s, they're all <div>s. If you loop over chalkboard's grandchildren, you'll find the images. Better is to loop over chalkboard.getElementsByTagName('img') rather than chalkboard.childNodes.
An alternative is to add a class to the images you want to fade in and loop over document.images.
Lastly, consider using addEventListener/attachEvent rather than the onload attribute. You can create wrappers around the browser native event registration methods to work with a consistent interface.
Code:
var addEventListenerTo, removeEventListenerFrom;
if (window.attachEvent) {
addEventListenerTo = function(element, eventName, listener) {
/* Proper implementation is non-trivial:
+ 'listener' should get the fired event as 1st argument.
+ 'eventName' is the DOM event name, which doesn't start with 'on'. 'attachEvent' expects event names
to begin with 'on'. Not all DOM events can be mapped to an IE event by prefixing 'on'.
+ IE will leak event listeners. When you add a listener, hook window's unload event to detach the listener.
+ Within the listener, 'this' must be bound to the object the listener is registered on, which is
+ 'element'.
+ The biggest issue (because it's not solvable using IE events) is that event capturing can't be supported.
Oh, well.
The more you implement, the more useful the library is. Not all of the features need to be
implemented immediately; they can be added as needed. Ain't separation of concerns grand?
*/
}
removeEventListenerFrom = function(element, event, listener) {
// implementation is almost trivial.
}
} else {
addEventListenerTo = function(element, event, listener) {
// implementation is trivial
}
removeEventListenerFrom = function(element, event, listener) {
// implementation is trivial
}
}
As this assignment has a close deadline, consider writing addEventListenerTo for the next assignment.