SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)

    Get all class instances?

    Is there a way to get all instances of a CSS class? I know that using the DOM you can get an element by it's ID (document.getElementById() ) or get an array of elements that are all the same tag (getElementsByTagName() ). Any help would be appreciated. No rush, I'm just curious.

    --Vinnie

  2. #2
    Sultan of Ping jofa's Avatar
    Join Date
    Mar 2002
    Location
    SvÝ■jˇ­
    Posts
    4,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Something like this?
    Code:
          function getElementsByClassName(className) 
          { 
            var arr = new Array(); 
            var elems = document.getElementsByTagName("*");
            for(var i = 0; i < elems.length; i++)
            {
              var elem = elems[i];
              var id = elem.getAttribute("id");
              var cls = elem.getAttribute("class");
              if(cls == className)
              {
                arr[arr.length] = id;
              }
            }
            return arr;
          }

  3. #3
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Once again jofa, you have proven your worthiness as a mentor. Thanks!

    --Vinnie

  4. #4
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Don't collection getters return an array of references, an not their IDs as strings? I mean, certainly each element with the designated className won't have it's own ID....
    Code:
    function getElementsByClassName( clsName ) 
    { 
    	var arr = new Array(); 
    	var elems = document.getElementsByTagName("*");
    	for ( var cls, i = 0; ( elem = elems[i] ); i++ )
    	{
    		if ( elem.className == clsName )
    		{
    			arr[arr.length] = elem;
    		}
    	}
    	return arr;
    }
    Last edited by beetle; Jan 14, 2003 at 15:24.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  5. #5
    Sultan of Ping jofa's Avatar
    Join Date
    Mar 2002
    Location
    SvÝ■jˇ­
    Posts
    4,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by beetle
    Don't collection getters return an array of references, an not their IDs as strings? ...
    Very true
    Don't remember what I was thinking (or wasn't thinking), maybe that it was "smarter" to return an array with the id:s (which is not true...)


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
  •