Cycle through all DIVs, please help

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

    #1

    Cycle through all DIVs, please help

    Hi to all,

    I have several DIVs in a page, each has a unique name. I'd need a method to
    cycle through all DIVs so that I can change their style. For example, let's
    say I need to set a red background to DIVs whose name begins with "a". This
    is the pseudo code:

    For Each Div in The Page
    If Div's name starts with 'a' then set Div's background to Red
    Next

    How can I translate this 'pseudocode' into real javascript code?
    Thank you!

    Marc


  • Martin Honnen

    #2
    Re: Cycle through all DIVs, please help



    Marc wrote:[color=blue]
    > I have several DIVs in a page, each has a unique name. I'd need a method to
    > cycle through all DIVs so that I can change their style. For example, let's
    > say I need to set a red background to DIVs whose name begins with "a". This
    > is the pseudo code:
    >
    > For Each Div in The Page
    > If Div's name starts with 'a' then set Div's background to Red
    > Next
    >
    > How can I translate this 'pseudocode' into real javascript code?[/color]

    <div> elements in HTML don't have a name attribute so you should use the
    id attribute e.g.
    <div id="a1">...</div>
    You can find out all the <div> elements in the document with
    var divs = document.getEle mentsByTagName( 'div');
    then loop
    for (var i = 0; i < divs.length; i++) {
    var div = divs[i];
    if (div.id.indexOf ('a') == 0 && div.style) {
    div.style.backg roundColor = 'red';
    }
    }

    --

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

    Comment

    • Ivo

      #3
      Re: Cycle through all DIVs, please help

      "Marc" wrote[color=blue]
      > I have several DIVs in a page, each has a unique name. I'd need a method[/color]
      to[color=blue]
      > cycle through all DIVs so that I can change their style. For example,[/color]
      let's[color=blue]
      > say I need to set a red background to DIVs whose name begins with "a".[/color]
      This[color=blue]
      > is the pseudo code:
      >
      > For Each Div in The Page
      > If Div's name starts with 'a' then set Div's background to Red
      > Next
      >
      > How can I translate this 'pseudocode' into real javascript code?
      > Thank you!
      >
      > Marc[/color]

      var els = document.getEle mentsByTagName( 'div');
      var i = els.length; while( i-- ) {
      if( els[i].indexOf('a') === 0 ) {
      els[i].style.backgrou nd = 'red';
      }
      }

      You could use an incrementing "for" loop, but a decrementing "while" is
      faster.
      HTH
      Ivo


      Comment

      • Ivo

        #4
        Re: Cycle through all DIVs, please help

        "Ivo"wrote[color=blue]
        > if( els[i].indexOf('a') === 0 ) {[/color]

        Too fast! That should of course have been:
        if( els[i].name.indexOf(' a') === 0 ) {
        if you are happy with named div's. I 'd use id's.
        Ivo


        Comment

        Working...