Monday, September 4, 2006

Get Element By Tag and Name attribute (getElementsByName sort of)

Cross browser function to get element(s) by "name" attribute.


/** getElementsByTagAndName: to avoid missing getElementsByName in IE */
function getElementsByTagAndName(tag, name) {
    var elem = document.getElementsByTagName(tag);
    var arr = new Array();
    for(i = 0, iarr = 0; i < elem.length; i++) {
        att = elem[i].getAttribute("name");
        if(att == name) {
            arr[iarr] = elem[i];
            iarr++;
        }
    }
    return arr;
}