...

View Full Version : getting all array count of all elements



jbot
04-22-2004, 05:48 PM
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? :confused:

liorean
04-22-2004, 06:22 PM
I'm wondering a bit about why you need this anyway, but the DOM way is document.getElementsByTagName('*').length

jbot
04-22-2004, 06:42 PM
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. :thumbsup:

btw: this is my 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 :rolleyes:

liorean
04-22-2004, 07:52 PM
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:
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).

jbot
04-23-2004, 10:42 AM
thx liorean :thumbsup:

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

Willy Duitt
04-23-2004, 11:03 AM
while(i-->0) == while i minus one is more than 0

.....Willy

jbot
04-23-2004, 11:10 AM
LOL Willy ... was just coming on to say I understood it. just the way it was written threw me :rolleyes:

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

jbot
04-23-2004, 11:38 AM
Anyway, have made an adjustment myself.

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



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. :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum