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:

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

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

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” ?

Howdy Vincent!

pf0 - such a childish mistake!
That was it.

Thank you, thank you, thank you!!!
:slight_smile:

Cheers,
Greg Bialowas

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

it was my pleasure.