Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts

    getting all array count of all elements

    Ok, in IE it's easy to get a count of all the elements in the page. You just say document.all.length.

    but this, of course, won't work in Moz. It used to be in NN that you could call document.tags but this is now deprecated.

    so, wot's the equivalent?

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    I'm wondering a bit about why you need this anyway, but the DOM way is document.getElementsByTagName('*').length
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #3
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by liorean
    document.getElementsByTagName('*').length
    I take it the * is meant as a wildcard? i knew of the method, but didn't realise you could do that.

    ok, thanks for that.

    btw: this is my code :

    Code:
    function getUsrErrors()
    {
    	var oDoc = document.getElementsByTagName('*') ;
    	var aObjs = new Array() ;
    	
    	alert("tags:\t" + oDoc.length) ;
    	
    	for (var i=0; i < oDoc.length; i++)
    	{
    		oTag = oDoc[i].tagName ;
    		oTag = oTag.toLowerCase() ;
    		
    		oCls = oDoc[i].className ;
    		
    		if (oTag=="div" || oTag=="span" || oTag=="td")
    		{
    			if (oCls.toLowerCase()=="errormsg") aObjs.push(oDoc[i]);
    		}
    	}
    	
    	if (aObjs != null)
    	{
    		for (var j=0; j < aObjs.length; j++)
    		{
    			if (aObjs[j].innerText != "")
    			{
    				// aObjs[j] ;
    				alert("has error") ;
    				break ;
    			}
    		}
    	}
    	
    }
    it's for a Java/Struts app, whereby certain elements are for alerting the user to validation errors. there are kinda better JSP ways to do this, but we haven't implemented them
    Last edited by jbot; 04-22-2004 at 05:46 PM.

  • #4
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Then I would suggest you to change the code slightly. First of all, in JavaScript the DOM has been implemented to be fast for indivudual element access, not for collection access. Storing document.getElementsByTagName('*') in a collection is going to be slower than accessing each element in it at a time. Have a look at this instead:
    Code:
    var
        oElm,
        aObjs=[],
        i=0;
    while((oElm=document.getElementsByTagName('*').item(i++)))
        if(/^(div|span|td)$/i.test(oElm.tagName))
            if(/^errormsg$/i.test(oElm.className))
                aObjs.push(oElm);
    
    i=aObjs.length;
    while(i-->0){
        if(''!=getTextContent(aObjs[i])){
            alert('has error');
            break;
        }
    }
    
    function getTextContent(oElm){
        var
            aText=[],
            i=oElm.childNodes.length,
            oElm2;
        while(i-->0)
            switch((oElm2=oElm.childNodes[i]).nodeType){
                case 1: // Element node
                    aText.unshift(getTextContent(oElm2));
                    break;
                case 3: // Text node or
                case 4: // CDATA section node
                    aText.unshift(oElm2.nodeValue);
                    break;
            }
        return aText.join('');
    }
    I've optimised your code a bit, as well as given you a DOM based alternative to the proprietary innerText (which Mozilla doesn't support).
    Last edited by liorean; 04-23-2004 at 01:59 PM.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #5
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thx liorean

    btw: wot does the "-->" operator mean. never seen that in JS before?

  • #6
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    while(i-->0) == while i minus one is more than 0

    .....Willy

  • #7
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts
    LOL Willy ... was just coming on to say I understood it. just the way it was written threw me

    i-->0 is the same as i-- > 0

  • #8
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Anyway, have made an adjustment myself.

    Because I just want to get the first error field, then this suffices:

    Code:
    function getUsrErrors()
    {
    	var oElm, aObjs=[], i=0;
    		
    	while ((oElm=document.getElementsByTagName('*').item(i++)))
    	{
    		if(/^(div|span|td)$/i.test(oElm.tagName))
    		{
    			if(/^errormsg$/i.test(oElm.className))
    			{
    				if ('' != getTextContent(oElm)) // has a error msg
    				{
    					alert('has error');
    					break;
    				}
    			}
    		}
    	}
    }
    thanks for the help, guys.


  •  

    Posting Permissions

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