PDA

View Full Version : [Javascript] Mutiple ID support



madcow
05-18-2008, 01:49 PM
Mutple Id Support

allows you to get elements by id like getelementbyid does except it allows you to get mutiple elements with the same id.

syntax:
document.getelementbyids("idname")[2].innerhtml = "hello"



(function(){
document.getElementByIds = function(Id){
var Tags = new Array();
docTags = document.getElementsByTagName('*');
for(i=0;i<docTags.length;i++){
if(docTags[i].getAttribute('id') == Id){
Tags[Tags.length] = docTags[i]
}
}
return Tags
};
})();

Master_script_maker
05-18-2008, 02:27 PM
Wouldn't it be more correct to have it "getelementsbyid"? Also, elements aren't supposed to have the same id, the id defines the element separately from the others. It would be more practical to have a get element by classname.

madcow
05-18-2008, 03:07 PM
both valid points:

1) Yeah it should really i'll go back and change it

2) CSS is abit of a sinner their as it lets you style more than one element with the same id. but yeah I agree but this is for the lazy people. but i agree.

Maybe a getelementsbyids wuld be better (gets elements that have one or another id)

jscheuer1
05-18-2008, 03:34 PM
The stricture, if you can call it that, of having only one element per page with a given id is part of the standard. So, although it may be possible to do what you have in some browsers, others may not respond as expected because they simply aren't required to. What I'm saying is that unless you were to test this in every browser, there would be no way to be reasonably sure if it really worked in them.

Conversely, if your code doesn't violate a standard, it is more reasonable to assume it will work cross browser without extensive testing.

Now your code looks to me like it should work in all modern browsers, but I've seen cases where just because something depended upon an illegal document structure, it wouldn't work in a browser like FF or Opera, even IE 7.