Results 1 to 4 of 4

Thread: [Javascript] Mutiple ID support

  1. #1
    Join Date
    Jan 2006
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default [Javascript] Mutiple ID support

    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"


    Code:
    (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
    	};
    })();
    Last edited by madcow; 05-18-2008 at 03:10 PM.

  2. #2
    Join Date
    Jun 2007
    Posts
    543
    Thanks
    3
    Thanked 78 Times in 78 Posts
    Blog Entries
    1

    Default

    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.
    [Jasme Library (Javascript Motion Effects)] My Site
    /\/\@§†ê® §©®¡þ† /\/\@|{ê®
    There are 10 kinds of people in the world, those that understand binary and those that don't.

  3. #3
    Join Date
    Jan 2006
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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)

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •