By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,818 Members | 2,089 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,818 IT Pros & Developers. It's quick & easy.

problems with the switch statement

P: n/a
hello
In the function below radtext is an array of 3 radio buttons with values set
to 1, 2 and 3. but variables starty and finishy are not returned.

************************************************** ***
function show(ele,invele1,invele2,invele3,invele4) {
switch (document.getElementById(radtext).value){
case 1:
var starty=2;
var finishy=14;
break
case 2:
var starty=13;
var finishy=30;
break
case 3:
var starty=30;
var finishy=32;
break
default:
var starty=2;
var finishy=32;
break
}

do something with starty and finishy
}
*************************************

There appears to be something wrong with the switch statement because when I
replace the switch with
var starty=2;
var finishy=32;

it works
Ive tried a few variations such as using 'if..' statement
eg

if(document.getElementById(radtext[0]).checked=true)
{
var starty=2;
var finishy=14;
}
else if (document.getElementById(radtext[1]).checked=true)
{
var starty=13;
var finishy=30;
}
etc etc .....

but still no luck

what is going wrong
Help grately appreciated

Ian

Aug 24 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
mantrid wrote on 24 aug 2006 in comp.lang.javascript:
hello
In the function below radtext is an array of 3 radio buttons with
values set to 1, 2 and 3. but variables starty and finishy are not
returned.

************************************************** ***
function show(ele,invele1,invele2,invele3,invele4) {
switch (document.getElementById(radtext).value){
case 1:
var starty=2;
var finishy=14;
break
case 2:
var starty=13;
var finishy=30;
break
case 3:
var starty=30;
var finishy=32;
break
default:
var starty=2;
var finishy=32;
break
why a break here ???
}

do something with starty and finishy
}
*************************************

There appears to be something wrong with the switch statement because
when I
There is nothing wrong with switch().

consider this:

======================
switch ('2'){
case 1:
a = 'e'
break
case 2:
a = 'x'
break
default:
a = 'd'
}

alert(a) // shows d

switch (2){
case 1:
a = 'e'
break
case 2:
a = 'x'
break
default:
a = 'd'
}

alert(a) //shows x
======================

A you see [and per specs]

1
the test is a ===, an absolute equality,
also type equality, and

alert( '2' === 2 )

will show false.

2
document.getElementById(radtext).value
returns a string !!!!
A solution [among many]:

case '1':
.....
case '2':
.....

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Aug 24 '06 #2

P: n/a
"Evertjan." <ex**************@interxnl.netwrote in message
news:Xn********************@194.109.133.242...
mantrid wrote on 24 aug 2006 in comp.lang.javascript:

There is nothing wrong with switch().

consider this:

======================
switch ('2'){
case 1:
a = 'e'
break
case 2:
a = 'x'
break
default:
a = 'd'
}

alert(a) // shows d

switch (2){
case 1:
a = 'e'
break
case 2:
a = 'x'
break
default:
a = 'd'
}

alert(a) //shows x
======================

A you see [and per specs]

1
the test is a ===, an absolute equality,
also type equality, and

alert( '2' === 2 )

will show false.

2
document.getElementById(radtext).value
returns a string !!!!
A solution [among many]:

case '1':
....
case '2':
....

You are correct. its not the switch. However your suggestion doesnt work
either.
switch ('2'){
case 1:
a = 'e'
break
case 2:
a = 'x'
break
default:
a = 'd'
}

alert(a) // shows d
It wasnt giving the default values the function wasnt working atall
Testing further I found putting radio buttons in a form and changing

document.getElementById(radtext).value
to
document.myform.radtext.value

as in code below
**************************

function show(ele,invele1,invele2,invele3,invele4) {

switch (document.myform.radtext.value){
case 1:
var starty=2;
var finishy=14;

etc
********************************
This does gives the default value, strange.
Also putting quotes around the numbers after 'case' makes no difference
still brings up default value

I tried substituting the numbers 1,2,3 in turn in place of the
'document.myform.radtext.value'
and it works

so seems like function is not retreiving the value of the radio button, and
so uses default
very strange

more info, this is the script for the buttons

******************************
<form name="myform">
<input name="radtext" id="radtext" type="radio" value="1" checked>Stored
Text
<input name="radtext" id="radtext" type="radio" value="2">Short Text
<input name="radtext" id="radtext" type="radio" value="3">Long Text
</form>
*********************************************
Aug 24 '06 #3

P: n/a
*** mantrid escribió/wrote (Thu, 24 Aug 2006 19:52:23 GMT):
function show(ele,invele1,invele2,invele3,invele4) {
switch (document.getElementById(radtext).value){
case 1:
var starty=2;
var finishy=14;
break
Two suggestions:

1) Form values are always strings. Do not trust automatic casting.
2) For God's sake, indent your code! ;-)

--
-+ http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
++ Mi sitio sobre programación web: http://bits.demogracia.com
+- Mi web de humor con rayos UVA: http://www.demogracia.com
--
Aug 24 '06 #4

P: n/a
mantrid wrote on 24 aug 2006 in comp.lang.javascript:
<form name="myform">
<input name="radtext" id="radtext" type="radio" value="1" checked>Stored
Text
<input name="radtext" id="radtext" type="radio" value="2">Short Text
<input name="radtext" id="radtext" type="radio" value="3">Long Text
</form>
Having more elements with the sama id is illegal HTML.

So javascript certainly cannot find a radio value this way.
Try:

<form name="myform"
onsubmit=
'radios = document.getElementsByName("radtext");
for(var i = 0; i < radios.length; i++)
if(radios[i].checked)
alert( radios[i].value);
return false;'
>
<input name="radtext" type="radio" value="1" checked>xxx<br>
<input name="radtext" type="radio" value="2">Short Text<br>
<input name="radtext" type="radio" value="3">Long Text<br>
<input type=submit>
</form>
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Aug 24 '06 #5

P: n/a

"Evertjan." <ex**************@interxnl.netwrote in message
news:Xn********************@194.109.133.242...
mantrid wrote on 24 aug 2006 in comp.lang.javascript:
<form name="myform">
<input name="radtext" id="radtext" type="radio" value="1" checked>Stored
Text
<input name="radtext" id="radtext" type="radio" value="2">Short Text
<input name="radtext" id="radtext" type="radio" value="3">Long Text
</form>

Having more elements with the sama id is illegal HTML.

So javascript certainly cannot find a radio value this way.
Try:

<form name="myform"
onsubmit=
'radios = document.getElementsByName("radtext");
for(var i = 0; i < radios.length; i++)
if(radios[i].checked)
alert( radios[i].value);
return false;'
<input name="radtext" type="radio" value="1" checked>xxx<br>
<input name="radtext" type="radio" value="2">Short Text<br>
<input name="radtext" type="radio" value="3">Long Text<br>
<input type=submit>
</form>
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

ok thanks for that it displays the value but I need that value in my
function and I dont want to have a submit button. so i modified my function
to:

******************************
function show(ele,invele1,invele2,invele3,invele4) {

var radios = document.getElementsByName("radtext");
for(var i = 0; i < radios.length; i++){
if(radios[i].checked) {
var checkedrad= radios[i].value);
}
}

switch (checkedrad){
case '1':
var starty=2;
var finishy=14;
break
:
:
etc

*********************
but this doesnt work. Is it that you can only use radiobuttons with a for
and a submit button?

Ian
Aug 24 '06 #6

P: n/a
Thanks Alvaro
will take your advice
The indenting is going to take some effort though
Ian
"Alvaro G. Vicario" <we*******@NOSPAMdemogracia.comwrote in message
news:1h*******************************@40tude.net. ..
*** mantrid escribió/wrote (Thu, 24 Aug 2006 19:52:23 GMT):
function show(ele,invele1,invele2,invele3,invele4) {
switch (document.getElementById(radtext).value){
case 1:
var starty=2;
var finishy=14;
break

Two suggestions:

1) Form values are always strings. Do not trust automatic casting.
2) For God's sake, indent your code! ;-)

--
-+ http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
++ Mi sitio sobre programación web: http://bits.demogracia.com
+- Mi web de humor con rayos UVA: http://www.demogracia.com
--

Aug 24 '06 #7

P: n/a
mantrid wrote on 25 aug 2006 in comp.lang.javascript:
ok thanks for that it displays the value but I need that value in my
function and I dont want to have a submit button.
You will need a way to trigger your function AFTER the radio is set by the
user. do you use a timeout then?

so i modified my
function to:

******************************
function show(ele,invele1,invele2,invele3,invele4) {
What does these ele,invele1,invele2,invele3,invele4 have to do with it?
>
var radios = document.getElementsByName("radtext");
for(var i = 0; i < radios.length; i++){
if(radios[i].checked) {
var checkedrad= radios[i].value);
Why the final ) ?
}
}

switch (checkedrad){
case '1':
var starty=2;
var finishy=14;
break
:
:
etc

*********************
but this doesnt work. Is it that you can only use radiobuttons with a
for and a submit button?
advice:
1 always explain what you mean by "does not work" in the c.l.j. NG
2 Do some debugging and show the results of that
3 Do not build a "finished product" but test seperate parts seperately.
4 properly indent your code
5 define ["var"] your local variables at the beginning of the function

try:

========== test.html ============
<input name="radtext" type="radio" value="1" checked>xxx<br>
<input name="radtext" type="radio" value="2">Short Text<br>
<input name="radtext" type="radio" value="3">Long Text<br>

<button onclick='show()'>SHOW</button>
<script type='text/javascript'>

function show() {

var radios = document.getElementsByName("radtext");

for(var i = 0; i < radios.length; i++)
if(radios[i].checked)
var checkedrad= radios[i].value;

switch (checkedrad){
case '1':
var starty = 's1';
break;
default:
var starty = 's???';
}

alert(starty);
};

</script>
======================

The switch() is probably not the best for newbies,
why not stick to:

var starty;
if ( checkedrad==1 ) // no '1' as this is == and not ===
starty = 's1';
else if ( checkedrad==2 )
starty = 's2';
else
starty = 's????';

==================

non-newbies probably wil prefer to use an array:

var starty = 's???'; // default
arr = ['skipzero',11,22,'33','fortyfour'];
if ( checkedrad<arr.length )
starty = arr( starty );

and not use the value='..' but the value of i:

var checkedrad;
.....

for(var i = 0; i < radios.length; i++)
if(radios[i].checked)
checkedrad = i+1;
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Aug 25 '06 #8

P: n/a

"Evertjan." <ex**************@interxnl.netwrote in message
news:Xn********************@194.109.133.242...
mantrid wrote on 25 aug 2006 in comp.lang.javascript:
ok thanks for that it displays the value but I need that value in my
function and I dont want to have a submit button.

You will need a way to trigger your function AFTER the radio is set by the
user. do you use a timeout then?

I use one of five buttons but not in a form as submit

so i modified my
function to:

******************************
function show(ele,invele1,invele2,invele3,invele4) {

What does these ele,invele1,invele2,invele3,invele4 have to do with it?

Thats to determine which column in a table is made visible. The function is
used by each of the 5 buttons mentioned above. Hence the 5 arguments in the
function. That bit works so I'm OK with that.
>

var radios = document.getElementsByName("radtext");
for(var i = 0; i < radios.length; i++){
if(radios[i].checked) {
var checkedrad= radios[i].value);

Why the final ) ?
I copied and modified

<form name="myform"
onsubmit=
'radios = document.getElementsByName("radtext");
for(var i = 0; i < radios.length; i++)
if(radios[i].checked)
alert( radios[i].value);

from your earlier post and forgot to delete the ) from the 'alert'. In fact
that was the problem. I was working very late last night and overlooked it
when checking the code after modifying it.
heres the whole working function anyway as you are interested. (any advice
about tidying it up, (besides what you already said), appreciated)

<script type="text/javascript" language="javascript">
<!--
function show(ele,invele1,invele2,invele3,invele4) {

var radios = document.getElementsByName("radtext");

for(var i = 0; i < radios.length; i++)
if(radios[i].checked)
var checkedrad= radios[i].value;

switch (checkedrad){
case '1':
var starty=2;
var finishy=14;
break
case '2':
var starty=13;
var finishy=30;
break
case '3':
var starty=30;
var finishy=32;
break
default:
var starty=2;
var finishy=32;

}

for(var y = starty; y <= finishy; y++){
var srcElement = document.getElementById(ele+y);
var srcElement1 = document.getElementById(invele1+y);
var srcElement2 = document.getElementById(invele2+y);
var srcElement3 = document.getElementById(invele3+y);
var srcElement4 = document.getElementById(invele4+y);

srcElement1.style.display='none';
srcElement2.style.display='none';
srcElement3.style.display='none';
srcElement4.style.display='none';

if(srcElement.style.display == "block") {
srcElement.style.display= 'none';
}
else {
srcElement.style.display='block';
}

}
}
//-->
</script>
Thanks again for your help
I didnt know that in javascript use of curly brackets is optional. Ive been
using php mostly, javascript is new to me.
Ian
Aug 25 '06 #9

P: n/a
JRS: In article <DU*****************@newsfe6-win.ntli.net>, dated Thu,
24 Aug 2006 22:39:31 remote, seen in news:comp.lang.javascript, mantrid
<ia********@virgin.netposted :
>Thanks Alvaro
will take your advice
The indenting is going to take some effort though
Firstly, read the newsgroup FAQ about preferred reply formats.
>"Alvaro G. Vicario" <we*******@NOSPAMdemogracia.comwrote in message
news:1h*******************************@40tude.net ...
>2) For God's sake, indent your code! ;-)
Provided that one abstains from putting unmatched { } in a line in
strings or in comment, ISTM that one could merge Tabber or T8 with Pack
in <URL:http://www.merlyn.demon.co.uk/js-quick.htm>, altering the line-
changing algorithm to one counting overall + { - } and then add enough
spaces at the beginning of the next line. Implemented there for initial
test as undocumented code :-
function Indt_(F) { var In = 0, InS = " ", C, J, K, L, P, Data
Data = F.Code.value.split("\n")
for (J=0 ; J < Data.length ; J++) {
L = Data[J].replace(/^\s+|\s+$/g, "") // Trim
for (K=0, P = "" ; K < In ; K++) P += InS // Build pad
for (K=0 ; K < L.length ; K++) { // Count +{ -}
C = L.charAt(K)
if (C=="{") In++ ; if (C=="}") In-- }
Data[J] = P + L } // Pad
F.Code.value = Data.join("\n") }
For refinement, I'd also like to indent where a statement overlaps a
line break.

Note that if the code must contain unmatched non-code { } on a line, one
can complete the match in comment.

Read the newsgroup and its FAQ.
--
© 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.
Aug 25 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.