Unfinished Loop

2

2nervous

I created the following function to change the visibility of nine
different table cells. It appears to only execute the first instead of
looping. How can I get it to loop through?


function toggleSelection(ID) {
var i=1
for (i=1;i<=9;i++)
{
var pID = "cell-" + i + "-id-" + ID;
var cell = document.getElementById(pID);

if(cell.style.visibility == "hidden" || !cell.style.visibility) {
cell.style.visibility = "visible";

}
else if(cell.style.visibility == "visible") {
cell.style.visibility = "hidden";

}
return false;
}


}
 
M

Michael Winter

I created the following function to change the visibility of nine
different table cells. It appears to only execute the first instead of
looping. How can I get it to loop through?

[snip]

Perhaps if you employed a better formatting style, the reason and
solution would become obvious:

function toogleSelection(id) {
for (var i = 1; i <= 9; ++i) {
var pID = 'cell-' + i + '-id-' + id,
cell = document.getElementById(pID);

if ((cell.style.visibility == 'hidden')
|| !cell.style.visibility) {
cell.style.visibility = 'visible';
} else if (cell.style.visibility == 'visible') {
cell.style.visibility = 'hidden';
}
return false;
}
}

That if statement could be greatly shortened to:

if (cell.style.visibility == 'visible') {
cell.style.visibility = 'hidden';
} else {
cell.style.visibility = 'visible';
}

or:

cell.style.visibility = (cell.style.visibility == 'visible')
? 'hidden' : 'visible';

Some feature detection wouldn't go amiss.

Mike
 
2

2nervous

Thanks for the reply, however I get the same result; the first toggles,
but the rest do not fire.



Michael said:
I created the following function to change the visibility of nine
different table cells. It appears to only execute the first instead of
looping. How can I get it to loop through?

[snip]

Perhaps if you employed a better formatting style, the reason and
solution would become obvious:

function toogleSelection(id) {
for (var i = 1; i <= 9; ++i) {
var pID = 'cell-' + i + '-id-' + id,
cell = document.getElementById(pID);

if ((cell.style.visibility == 'hidden')
|| !cell.style.visibility) {
cell.style.visibility = 'visible';
} else if (cell.style.visibility == 'visible') {
cell.style.visibility = 'hidden';
}
return false;
}
}

That if statement could be greatly shortened to:

if (cell.style.visibility == 'visible') {
cell.style.visibility = 'hidden';
} else {
cell.style.visibility = 'visible';
}

or:

cell.style.visibility = (cell.style.visibility == 'visible')
? 'hidden' : 'visible';

Some feature detection wouldn't go amiss.

Mike
 
2

2nervous

I had noticed before my post that when I removed " return false; " that
a 4 of the cells would toggle before the error.

The problem was there was not a 5th cell. The error was because of the
missing object. My bad! Renaming and adjusting my code solved the
problem.


Thanks for the reply, however I get the same result; the first toggles,
but the rest do not fire.



Michael said:
I created the following function to change the visibility of nine
different table cells. It appears to only execute the first instead of
looping. How can I get it to loop through?

[snip]

Perhaps if you employed a better formatting style, the reason and
solution would become obvious:

function toogleSelection(id) {
for (var i = 1; i <= 9; ++i) {
var pID = 'cell-' + i + '-id-' + id,
cell = document.getElementById(pID);

if ((cell.style.visibility == 'hidden')
|| !cell.style.visibility) {
cell.style.visibility = 'visible';
} else if (cell.style.visibility == 'visible') {
cell.style.visibility = 'hidden';
}
return false;
}
}

That if statement could be greatly shortened to:

if (cell.style.visibility == 'visible') {
cell.style.visibility = 'hidden';
} else {
cell.style.visibility = 'visible';
}

or:

cell.style.visibility = (cell.style.visibility == 'visible')
? 'hidden' : 'visible';

Some feature detection wouldn't go amiss.

Mike
 
J

J R Stockton

Sat said:
cell.style.visibility = (cell.style.visibility == 'visible')
? 'hidden' : 'visible';


It's a pity that the property uses a "word-string" rather than something
which will pass for a Boolean. With F.X0 being an <Input type=text>,
consider

Z = ! (F.X0.value ^= 1)

which, without repetition in the code, toggles F.X0.value between 0 & 1
and sets Z alternately to true or false. Presumably it would work with
any other "argument".
 
M

Michael Winter

J said:
It's a pity that the property uses a "word-string" rather than something
which will pass for a Boolean.

If there were only two values for the property, perhaps. As it is, there
are four ('collapse' and 'inherit' are the other two).

[snip]

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top