Forum Moderators: open

Message Too Old, No Replies

help with easy function

         

staho77

8:15 am on Aug 9, 2005 (gmt 0)

10+ Year Member



Hello
I'm new to JS and need help with one row in my function.
Now it lokks as follows:

function ChangeTypIm(){
if (form2.typImCh.checked==true) {
document.getElementById("form2_id1").style.visibility = "visible";
document.getElementById("form2_id2").style.visibility = "visible";
document.getElementById("form2_id3").style.visibility = "visible";
document.getElementById("form2_id4").style.visibility = "visible";
document.getElementById("form2_id5").style.visibility = "visible";
document.getElementById("form2_id6").style.visibility = "visible";
} else {
document.getElementById("form2_id1").style.visibility = "hidden";
document.getElementById("form2_id2").style.visibility = "hidden";
document.getElementById("form2_id3").style.visibility = "hidden";
document.getElementById("form2_id4").style.visibility = "hidden";
document.getElementById("form2_id5").style.visibility = "hidden";
document.getElementById("form2_id6").style.visibility = "hidden";
}
}

How can I replace document.getElementById.... with FOR cycle?

Thanks

staho77

8:38 am on Aug 9, 2005 (gmt 0)

10+ Year Member



I got it, bu I dont understand, when to use
for(b=0;b<2;b++)
and when
for(int b=0;b<2;b++)

RonPK

10:35 am on Aug 9, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It all has to do with the scope of the variable. If you're using the loop inside a function, it's normally best to write it like this:

for (var i = 0; i < 10; i++) {
// code here
}

This way, the variable i will not be available outside the scope of the function. Nor will it interfere with a variable i that may exist outside the function.

(int is not a javascript statement)