473,378 Members | 1,522 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Unfinished Loop

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;
}
}

Oct 21 '06 #1
5 1400
2n******@gmail.com wrote:
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
Oct 21 '06 #2
Thanks for the reply, however I get the same result; the first toggles,
but the rest do not fire.

Michael Winter wrote:
2n******@gmail.com wrote:
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
Oct 21 '06 #3
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.
2n******@gmail.com wrote:
Thanks for the reply, however I get the same result; the first toggles,
but the rest do not fire.

Michael Winter wrote:
2n******@gmail.com wrote:
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
Oct 21 '06 #4
In message <k0*******************@text.news.blueyonder.co.uk> , Sat, 21
Oct 2006 01:49:04, Michael Winter <m.******@blueyonder.co.ukwrites
>
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".

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Oct 21 '06 #5
J R Stockton wrote:
In message <k0*******************@text.news.blueyonder.co.uk> , Sat, 21
Oct 2006 01:49:04, Michael Winter <m.******@blueyonder.co.ukwrites
> 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.
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
Oct 21 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Charles Alexander | last post by:
Hello I am new to php & MySQL - I am trying to retrieve some records from a MySQL table and redisplay them. The data in list form looks like this: Sample_ID Marker_ID Variation ...
7
by: Alf P. Steinbach | last post by:
I have to shop Christmas gifts (and clean up... ;-)) before leaving on Christmas holiday tomorrow, where I'll effectively be without a network connection until January 6th, so I just posted what I...
5
by: Martin Schou | last post by:
Please ignore the extreme simplicity of the task :-) I'm new to C, which explains why I'm doing an exercise like this. In the following tripple nested loop: int digit1 = 1; int digit2 = 0;...
2
by: Alex | last post by:
Compiler - Borland C++ 5.6.4 for Win32 Copyright (c) 1993, 2002 Borland Linker - Turbo Incremental Link 5.65 Copyright (c) 1997-2002 Borland Platform - Win32 (XP) Quite by accident I stumbled...
3
by: Ben R. | last post by:
In an article I was reading (http://www.ftponline.com/vsm/2005_06/magazine/columns/desktopdeveloper/), I read the following: "The ending condition of a VB.NET for loop is evaluated only once,...
32
by: cj | last post by:
When I'm inside a do while loop sometimes it's necessary to jump out of the loop using exit do. I'm also used to being able to jump back and begin the loop again. Not sure which language my...
2
by: TheSteph | last post by:
Hi ! I have a question : the ComboBox control have NO single border, and height is at least 1 pixel higher than TextBox. There is absolutely NO good reason for making a ComboBox looking that...
2
by: Vili | last post by:
Hi all I am having problems with creating an functional questionnaire with asp.net 2.0 and MSSQL 2005 database. I have a table with field id (key & auto int), clientId (int), QuestionId...
2
ADezii
by: ADezii | last post by:
If you are executing a code segment for a fixed number of iterations, always use a For...Next Loop instead of a Do...Loop, since it is significantly faster. Each pass through a Do...Loop that...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.