How to interate over DIVs?

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

    #1

    How to interate over DIVs?

    Only the first <div> executes in the following code. I'd like the
    second to execute as well. This code is dynamically generated so I
    won't know exactly how many <div> tags will be needed. If they can all
    use the same ID/Name that shouldn't be a problem. Also, instead of
    using a button, I'll execute fadetext() on page load.

    I've posted a suggestion below the code from the sytelsheets group,
    which involves arrays and looping that I'd like help with.


    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">


    <html>
    <head>
    <title>Untitled </title>
    </head>

    <body>
    <script type="text/javascript">
    /* grays
    RGB(184,184,184 ) #B8B8B8
    RGB(192,192,192 ) #C0C0C0
    RGB(200,200,200 ) #C8C8C8
    RGB(208,208,208 ) #D0D0D0
    RGB(216,216,216 ) #D8D8D8
    RGB(224,224,224 ) #E0E0E0
    RGB(232,232,232 ) #E8E8E8
    RGB(240,240,240 ) #F0F0F0
    RGB(248,248,248 ) #F8F8F8
    RGB(255,255,255 ) #FFFFFF
    */
    hex=224 // Initial color value.

    function fadetext(){
    if(hex>0) { //If color is not black yet
    hex-=11; // increase color darkness
    document.getEle mentById("sampl e").style.color =
    "rgb("+hex+","+ hex+","+hex+")" ;
    //setTimeout("fad etext()",20);
    }
    }

    </script>

    <div id="sample" style="width:10 0%"><h3>John slowly faded into
    view</h3></div>
    This area will not fade out.<br>
    <div id="sample" style="width:10 0%"><h3>Anoth er fade occurs
    here.</h3></div>
    <button onClick="fadete xt()">Fade Text</button>

    </body>
    </html>


    This suggestion came from the sytelsheets group. This is what I'm
    wondering how to do - the array and looping part. Any suggestions?

    Thanks,
    Brett

    ---------------

    Well they can't.
    IDs must be unique and NAME is not a valid attribute for DIVs.


    Give them all the same CLASS. You can then use
    getElementsByTa gName('DIV') to create an array of all the DIVs in the
    page and then go through that array checking that the .className
    property matches the class you want to apply the fade to. Further
    questions on the DOM/JavaScript aspects would be better asked in
    comp.lang.javas cript

  • Martin Honnen

    #2
    Re: How to interate over DIVs?



    brett wrote:
    [color=blue]
    > Only the first <div> executes in the following code. I'd like the
    > second to execute as well. This code is dynamically generated so I
    > won't know exactly how many <div> tags will be needed. If they can all
    > use the same ID/Name that shouldn't be a problem.
    > <div id="sample" style="width:10 0%"><h3>John slowly faded into
    > view</h3></div>
    > This area will not fade out.<br>
    > <div id="sample" style="width:10 0%"><h3>Anoth er fade occurs
    > here.</h3></div>[/color]

    The id attribute value of an element should be unique in the HTML
    document so what you have is not a good idea.

    As for iterating over div elements, you can find elements by their tag
    name as follows:
    var divs;
    if (document.getEl ementsByTagName &&
    (divs = document.getEle mentsByTagName( 'div'))) {
    for (var i = 0; i < divs.length; i++) {
    // access divs[i] here
    }
    }

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/

    Comment

    • brett

      #3
      Re: How to interate over DIVs?

      I have this working now in IE6, Firefox, and NS7. I'm not checking for
      browser version. I'm just doing this:
      document.getEle mentById(nodeOb jectImg).style. MozOpacity=.20;
      document.getEle mentById(nodeOb jectImg).filter s.alpha.opacity =15;

      No errors are being thrown. Is the browser checking necessary? What
      would that look like?

      <html>
      <head>
      <title>Untitled </title>
      </head>

      <body>
      <script type="text/javascript">
      hex=224 // Initial color value.

      function fadetext(){
      var nodeObjectTag
      var nodeObjectImg
      for (var i = 1; i < 3; i++)
      {
      nodeObjectTag = "sample"+i;
      document.getEle mentById(nodeOb jectTag).style. color=
      "rgb("+hex+","+ hex+","+hex+")" ;
      }
      i=1
      for (var i = 1; i < 2; i++)
      {
      nodeObjectImg = "imgsample" +i;

      document.getEle mentById(nodeOb jectImg).style. MozOpacity=.20;
      document.getEle mentById(nodeOb jectImg).filter s.alpha.opacity =15;

      }
      }
      </script>

      <div id="sample1" style="width:10 0%"><h3>John slowly faded into
      view</h3></div>
      <a href="x">Here is a problem link</a><br>
      <!--style="filter:a lpha(opacity=10 0)"
      crossobj.style. MozOpacity-->
      <img src="http://www.google.com/images/logo.gif" alt=""
      name="imgsample 1" id="imgsample1 " border="0"
      style="filter:a lpha(opacity=10 0);-moz-opacity:1"><br>
      <div id="sample2" style="width:10 0%"><h3>Anoth er fade occurs
      here.</h3></div>
      <br>
      <a href="x">Here is another problem link</a>
      <br><br>
      <button onClick="fadete xt()">Fade Text</button>

      </body>
      </html>


      Thanks,
      Brett

      Comment

      Working...