...

View Full Version : newbie. RTFM failure, collections vs. arrays, converting



Roy Gardiner
06-03-2004, 11:33 AM
I use getElementsByTagName to return a 'collection of descendant element nodes'. I can then process this as if it were an array:

toc = document.getElementById("tableOfContents")
allToc = toc.getElementsByTagName("*")
for (i = 0; i < allToc.length; i++) {
allToc[i].style.display = "none"
} I would expect to be able to extract the whole of style.display using join(), since it acts like an array, but I can't. What must I do?

I suspect that I fundamentally misunderstand data types, especially 'collections' :mad:. A pointer to the correct part of the FM would be appreciated.

Vladdy
06-03-2004, 04:56 PM
Not sure what is the problem with your code, link to a page would be helpful, BUT there is one thing that is fundumentally wrong in your approach:
If your goal is to hide every single element in one with id="tableOfContents", why can not you hide that element itself and be done with it:
document.getElementById("tableOfContents").style.display = 'none';
:confused:

Roy Gardiner
06-03-2004, 06:32 PM
Not sure what is the problem with your code, link to a page would be helpful, OK I'm trying to follow guidlines by only citing very small snippets of code
BUT there is one thing that is fundumentally wrong in your approach:If your goal is to hide every single element in one with id="tableOfContents", why can not you hide that element itself and be done with it:document.getElementById("tableOfContents").style.display = 'none'; :confused: I have not explained myself properly, sorry; it's not the 'table of contents' I wish to show or hide, but various of the elements within it. The variable 'allToc', as shown in the code, acts like an array of those elements; except that I don't seem to be able to convert it to a string
allToc.style.display.join() I can put up a full page if that's the way it's done here so that you can just cut&paste to try it. Thanks for your patience and help.

liorean
06-03-2004, 06:53 PM
First of all, collections are not arrays. They might share two common properties with arrays, being the hash accessor syntax and the length property, but that's where the similarities end. Besides, the collection is the return value of the getElementsByTagName method, not the style, and certainly not the display properties. If you want to change all elements in the collection, you must loop through the collection and change them one at a time.
var
allToC=document.getElementById("tableOfContents").getElementsByTagName('*');
elm,
i=0;
while((elm=allToC.item(i++)))
elm.style.display='none';

jkd
06-03-2004, 09:25 PM
They are closer than you might believe, though:


HTMLOptionsCollection.prototype.sort = Array.prototype.sort;
HTMLOptionsCollection.prototype.aSort = function() {
this.sort( function(a,b) {
return a.value<b.value;
});
}


Or something like that... (lost my example) in Mozilla. Should get interesting results when calling refToSelect.options.aSort(). Similar things with push()/pop(). At least, in older builds.

Roy Gardiner
06-03-2004, 09:53 PM
First of all, collections are not arrays. They might share two common properties with arrays.. OK, thanks, I had read that collections and arrays are different, but had thought that there might be an equivalent to JOIN etc, since it seems so reasonable. Clearly not.

Thanks for your help, everyone, I appreciate it.

jkd
06-04-2004, 01:52 AM
Once again, another Mozilla curiousity (tested in Firefox 0.8):



HTMLOptionElement.prototype.toString = function() { return this.text }
HTMLOptionsCollection.prototype.join = Array.prototype.join;

alert(someSelect.options.join("\n")); // returns all the text's on newlines



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum