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 7 of 7
  1. #1
    New Coder
    Join Date
    Jan 2004
    Location
    London, England
    Posts
    95
    Thanks
    0
    Thanked 0 Times in 0 Posts

    newbie. RTFM failure, collections vs. arrays, converting

    I use getElementsByTagName to return a 'collection of descendant element nodes'. I can then process this as if it were an array:
    Code:
    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' . A pointer to the correct part of the FM would be appreciated.

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    Nashua, NH
    Posts
    1,724
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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';
    Vladdy | KL
    "Working web site is not the one that looks the same on common graphical browsers running on desktop computers, but the one that adequately delivers information regardless of device accessing it"

  • #3
    New Coder
    Join Date
    Jan 2004
    Location
    London, England
    Posts
    95
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Vladdy
    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';
    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
    Code:
    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.

  • #4
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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.
    Code:
    var
        allToC=document.getElementById("tableOfContents").getElementsByTagName('*');
        elm,
        i=0;
    while((elm=allToC.item(i++)))
         elm.style.display='none';
    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
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    They are closer than you might believe, though:
    Code:
    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.

  • #6
    New Coder
    Join Date
    Jan 2004
    Location
    London, England
    Posts
    95
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by liorean
    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.

  • #7
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    Once again, another Mozilla curiousity (tested in Firefox 0.8):

    Code:
    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


  •  

    Posting Permissions

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