There are many dollar sign functions out there, especially in the Javascript packages such as Prototype or MooFX. Peter-Paul Koch over at Quirksmode offers a cross-browser version of document.getElementById. Let’s combine all the goodness from each of these to make a souped-up $() dollar sign function that works in most browsers, and is not dependant on the DOM implementation of just one. It’s actually 2 functions, the $() and then a helper function.
function $() {
var elements = new Array();
for (var i = 0; i < arguments.length; i++) {
var element = arguments[i];
if (typeof element == 'string')
element = getElementByIdCompat(element);
if (arguments.length == 1)
return element;
elements.push(element);
}
return elements;
}
function getElementByIdCompat(name) {
if (document.getElementById) {
return document.getElementById(name);
} else if (document.all) {
return document.all[name];
} else if (document.layers) {
var theobj = {};
theobj = document.layers[name];
theobj.style = document.layers[name];
return theobj;
}
}
Post a Comment