SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: JS "for" loop

Hybrid View

  1. #1
    SitePoint Addict greg76's Avatar
    Join Date
    Aug 2004
    Location
    Poland
    Posts
    262
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    JS "for" loop

    Hey Guys,

    been fighting with few lines of code for the last half hour and I am about to explode:
    I have this little function:
    Code:
    function showImg(activ, no) {
    
          for (x = 0; x <= no; x++)  {
            document.getElementById('pf'+x).className = 'toggleHide';
          }
          
        document.getElementById('pf' + activ).className = 'toggleShow';
    }
    and when a link is clicked (which should hide all the divs at first and then show only the current one = I know it's primitive but I am no js programmer at all!!) the error console tells me "document.getElementById('pf'+x) is null".
    What am I doing wrong?

    when I specify the instances, one by one
    Code:
      document.getElementById('pf1').className = 'toggleHide';
      document.getElementById('pf2').className = 'toggleHide';
      document.getElementById('pf3').className = 'toggleHide';
      document.getElementById('pf4').className = 'toggleHide';
      document.getElementById('pf5').className = 'toggleHide';
      
      document.getElementById('pf'+activ).className = 'toggleShow';
    everything works just fine.

    Any help?

    Thank you in advance!

    Cheers,
    Greg

  2. #2
    SitePoint Guru Vincent Puglia's Avatar
    Join Date
    Feb 2000
    Location
    where the World once stood
    Posts
    700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Greg,

    Don't see anything offhand, so debug:


    alert(activ)
    for (x = 0; x <= no; x++) {
    alert("x=" + x + "no = " + no)
    document.getElementById('pf'+x).className = 'toggleHide';
    }

    document.getElementById('pf' + activ).className = 'toggleShow';
    }

    Do you have a "pf0" ?
    Where the World Once Stood
    the blades of grass
    cut me still

  3. #3
    SitePoint Addict greg76's Avatar
    Join Date
    Aug 2004
    Location
    Poland
    Posts
    262
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Howdy Vincent!

    pf0 - such a childish mistake!
    That was it.

    Thank you, thank you, thank you!!!



    Cheers,
    Greg Bialowas

  4. #4
    SitePoint Guru Vincent Puglia's Avatar
    Join Date
    Feb 2000
    Location
    where the World once stood
    Posts
    700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by greg76 View Post
    Howdy Vincent!

    pf0 - such a childish mistake!
    That was it.

    Thank you, thank you, thank you!!!



    Cheers,
    Greg Bialowas

    de nada, don't mention it, forget about it.

    it was my pleasure.
    Where the World Once Stood
    the blades of grass
    cut me still


Bookmarks

Posting Permissions

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