[thelist] [DHTML] Elements with the same ID
Peter-Paul Koch
gassinaumasis at hotmail.com
Wed Mar 19 04:22:38 CST 2003
>Each call to getElementsByTagName() likely generates a unique temporary
>array of the appropriate elements, which wastes memory and processor
>cycles. Thus for better performance (and cleaner code):
>
>function hideall(hiddenContentElement, hiddenContentClass)
>{
> var hiddenElements =
>document.getElementsByTagName(hiddenContentElement);
> for (i=0; i<hiddenElements .length; i++)
> if (hiddenElements .item(i).className ==
>hiddenContentClass)
> hiddenElements .item(i).style.display = "none";
>}
>
>That said, this is an excellent implementation using the class attribute as
>it is intended. Good work David!
One remark: using item() is completely unnecessary in JavaScript, you
*never* need it. It's for reading out specific items of a NodeList, but
JavaScript automatically makes any NodeList an array. So
hiddenElement.item(i)
should become
hiddenElement[i]
Otherwise the code is of course correct.
--------------------------------------------------
ppk, freelance web developer
Interaction, copywriting, JavaScript, integration
http://www.xs4all.nl/~ppk/
Column "Keep it Simple": http://www.digital-web.com/columns/keepitsimple/
W3C DOM Compatibility Table, expanded & updated
http://www.xs4all.nl/~ppk/js/index.html?version5.html
--------------------------------------------------
_________________________________________________________________
The new MSN 8: smart spam protection and 2 months FREE*
http://join.msn.com/?page=features/junkmail
More information about the thelist
mailing list