dcsimg
www.webdeveloper.com
Results 1 to 11 of 11

Thread: document.getAllElements() - or equivalent

  1. #1
    Join Date
    Feb 2005
    Location
    USA
    Posts
    1,534
    ..
    Last edited by BigMoosie; 11-24-2005 at 03:56 AM.

  2. #2
    Join Date
    Feb 2005
    Location
    BC, Canada
    Posts
    1,098
    PHP Code:
    function whatever () {
     
    test = new Array();
     for (
    i in document) {
      array[
    test.length] = document[i];
     }
     return array;


  3. #3
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,279
    Code:
    array = document.getElementsByTagName ('*')
    See http://www.w3.org/TR/1998/REC-DOM-Level-1-19981001/ .
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web

  4. #4
    Join Date
    Feb 2005
    Location
    USA
    Posts
    1,534
    ..
    Last edited by BigMoosie; 11-24-2005 at 03:56 AM.

  5. #5
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,279
    I don't know the answer to your question but I do see a slight problem with your method. In HTML the "class" attribute takes a space separated list of classes. You might want to do something more like:

    Code:
    document.getElementsByClassName = function(s){
    	var a = new Array(), e, i= 0;
    	while (e = document.getElementsByTagName ('*')[i++]) {
    		var r = new RegExp ('\bs\b', i);
    		if (r.test (e.className)) a.push (e);
    	}
    	return a;
    }
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web

  6. #6
    Join Date
    Feb 2005
    Location
    USA
    Posts
    1,534
    ..
    Last edited by BigMoosie; 11-24-2005 at 03:56 AM.

  7. #7
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,279
    The "\b" in the regulax expression is suposed to match word boundries.
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web

  8. #8
    Join Date
    Feb 2005
    Location
    USA
    Posts
    1,534
    ..
    Last edited by BigMoosie; 11-24-2005 at 03:56 AM.

  9. #9
    Join Date
    Jun 2003
    Location
    here
    Posts
    4,551
    I got a working edit:

    Code:
    document.getElementsByClassName=function(){
    var r = new RegExp('/\b'+arguments[0]+'\b/', 'i');
      var a = new Array();
      var e;
      var i = 0;
    while (e = document.getElementsByTagName ('*')[i++]){
    		if (r.test (e.className)) a.push (e);
    	}
    	return a;
    }
    I changed:
    the regular expression to not be evaluated every time.
    the regular expression not to match the letter s, but the argument.
    i to be in quotes.

    You could try doing those one after another untill you find the error, or just use this one.
    Last edited by scragar; 05-14-2005 at 07:05 AM.
    If you are using PHP please use the [PHP] and [/PHP] forum tags for highlighting...
    The same applies to HTML and the forums [HTML][/HTML] tags.

  10. #10
    Join Date
    Feb 2005
    Location
    USA
    Posts
    1,534
    ..
    Last edited by BigMoosie; 11-24-2005 at 03:56 AM.

  11. #11
    Join Date
    Jun 2003
    Location
    here
    Posts
    4,551
    Code:
    <script>
    <!--
    document.getElementsByClassName=function(){
      var a = new Array();
      var e;
      var i = 0;
      while (e = document.getElementsByTagName ('*')[i++]){
        if(e.className == arguments[0]){
          a.push (e);
        };
      };
      return a;
    };
    window.onload = function(){
      alert(document.getElementsByClassName("red").length);
    };
    //-->
    </script>
    </head>
    <body>
    <p class="red">text</p>
    <span>blah</span>
    </body>
    tested in IE and firefox only.
    Last edited by scragar; 05-16-2005 at 03:12 AM.
    If you are using PHP please use the [PHP] and [/PHP] forum tags for highlighting...
    The same applies to HTML and the forums [HTML][/HTML] tags.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles