Hide Divs

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • shapper

    Hide Divs

    Hello,

    I created a function to show a div given its id and hide the other
    divs (defined in a div):

    function show(id) {
    //var e = document.getEle mentsByTagName( "div");
    var e = {"home", "contact", "products", "photos"};
    for (var i = 0; i < e.length; i++) {
    e[i].style.display = 'none';
    if (e[i].id == id)
    e[i].style.display = 'block';
    }
    }

    This is not working. What am I doing wrong?

    It works if I use:
    var e = document.getEle mentsByTagName( "div");

    But I wan to hide the divs which id's are in the list and show the one
    in that list that has the given id.

    Thanks,
    Miguel
  • Dan Rumney

    #2
    Re: Hide Divs

    shapper wrote:
    I created a function to show a div given its id and hide the other
    divs (defined in a div):
    >
    function show(id) {
    //var e = document.getEle mentsByTagName( "div");
    var e = {"home", "contact", "products", "photos"};
    for (var i = 0; i < e.length; i++) {
    e[i].style.display = 'none';
    if (e[i].id == id)
    e[i].style.display = 'block';
    }
    }
    >
    This is not working. What am I doing wrong?
    >
    It works if I use:
    var e = document.getEle mentsByTagName( "div");

    Your variable e is simply an array of strings, not elements.

    Why did you comment out the 'getElementsByT agName' line?

    Comment

    • Thomas 'PointedEars' Lahn

      #3
      Re: Hide Divs

      Dan Rumney wrote:
      shapper wrote:
      >I created a function to show a div given its id and hide the other
      >divs (defined in a div):
      >>
      > function show(id) {
      > //var e = document.getEle mentsByTagName( "div");
      > var e = {"home", "contact", "products", "photos"};
      > for (var i = 0; i < e.length; i++) {
      > e[i].style.display = 'none';
      > if (e[i].id == id)
      > e[i].style.display = 'block';
      > }
      > }
      >>
      >This is not working. What am I doing wrong?
      >>
      >It works if I use:
      >var e = document.getEle mentsByTagName( "div");
      >
      Your variable e is simply an array of strings, not elements.
      There is no variable, there is a syntax error. In contrast to Java, Array
      initializers are delimited by `[' and `]' in ECMAScript implementations .
      `{' and `}' delimit Object initializers and BlockStatements instead.

      Since an expression is expected right-hand side here, an Object initializer
      would be expected. However, the syntax for that is {property: value, ...}
      or {"property": value, ...} instead.
      Why did you comment out the 'getElementsByT agName' line?
      I don't know either. Those two lines are not the least equivalent. The
      former would return a reference to a NodeList host object, the latter to a
      native Array object if it was properly delimited.


      PointedEars
      --
      realism: HTML 4.01 Strict
      evangelism: XHTML 1.0 Strict
      madness: XHTML 1.1 as application/xhtml+xml
      -- Bjoern Hoehrmann

      Comment

      Working...