JRS: In article <qY****************@newsfe6-win.ntli.net>, dated Sun, 3
Sep 2006 13:41:42 remote, seen in news:comp.lang.javascript, mantrid
<ia********@virgin.netposted :
>Hello
can anyone see why the following function is not working.
The extended version, where I take out the first 'for' loop and substitute
in all the 'cases' works.
Sorry if I'm being dumb but Im still learning javascript
***************************************
function answernum() {
var selnumber =
document.questform.answno.options[document.questform.answno.selectedIndex].v
alue;
switch (selnumber){
for(var n = 2; n <= 10; n++){
case n:
for(var x = 7; x <= 7++; x++){
var answerrow = document.getElementById("row"+x);
answerrow.style.display='';
}
break
}
default:
for(var x = 7; x <= 15; x++){
var answerrow = document.getElementById("row"+x);
answerrow.style.display='none';
}
}
}
You're evidently writing far too much code at once in the pious hope
that it can be understood, then trying to fond out why it cannot.
And 7++ cannot be right : code should be copy'n'pasted, not manually
transcribed.
If one could tell what you actually want, one could probably suggest a
much better way to do it.
function answernum() { var x, n, answerrow
var selnumber = + document.questform.answno.
options[document.questform.answno.selectedIndex].value
// selnumber will now be a Number not a String.
for (x = 7; x <= 15; x++) {
answerrow = document.getElementById("row"+x)
answerrow.style.display = (x selnumber) ? '' : 'none'
}
}
may be more like what you need.
Always write code in pieces small enough that they are probably correct,
and then test the pieces before writing more.
Your function would be easier to test if selnumber were a parameter, and
the loop body were replaced by
alert("row" + x + " " + ( (x selnumber) ? '' : 'none' ) )
as you would then be testing the management and manipulation parts
independently.
--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<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.