473,399 Members | 2,159 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,399 software developers and data experts.

problems with the switch statement

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
9 1493
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
"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
*** 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
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

"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
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
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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Myster Ious | last post by:
Polymorphism replaces switch statements, making the code more compact/readable/maintainable/OO whatever, fine! What I understand, that needs to be done at the programming level, is this: a...
35
by: Thomas Matthews | last post by:
Hi, My son is writing a program to move a character. He is using the numbers on the keypad to indicate the direction of movement: 7 8 9 4 5 6 1 2 3 Each number has a direction except...
7
by: Jonathan | last post by:
Hey everyone! I am pretty new to C++, and I am just playing around with some things that I have learned so far (trying to put them to use before I continue on with the book). In one section of my...
3
by: Mark Morton | last post by:
I'm writing an if statement for a UK credit card form validation script. Users who specify that their card is Switch need to enter either the issue number or the 'valid from' date. I'm trying to...
13
by: Yodai | last post by:
Hi all.... I have a little problem that's driving me nuts. I can't seem to make any sense of it. I have this small webserver that substitutes some data from a page when finds a substitution...
14
by: Wescotte | last post by:
I have an application that uses several file formats for similar data. So I've created various php files for each format containing the same functions which produce the same end result. Now I...
12
by: | last post by:
Is it fine to call another method from Switch? Eg. Switch (stringVar) { case ("a"): somVar = "whatever"; Another_Method(); //call another method return;
4
by: huzzaa | last post by:
I am using a switch statement that will decide from a random number what message to display. I want the random number to be between 0 and 100 and then if the number is say between 1 and 10 to...
13
by: Satya | last post by:
Hi everyone, This is the first time iam posting excuse me if iam making any mistake. My question is iam using a switch case statement in which i have around 100 case statements to compare. so...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.