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

if vs switch

Hello, Aren't they same conditional statment to show the resut?
but somehow "switch" condtional stmt always gets the default value when I excute?
what did I wrong?

<script language="JavaScript">
<!--
function showtype(obj) {
if (obj.options[obj.selectedIndex].value == "137")
{
document.getElementById('secID1').style.visibility = 'visible'
document.getElementById('brhID1').style.visibility = 'hidden'
document.getElementById('divID1').style.visibility = 'hidden'
document.getElementById('depID1').style.visibility = 'hidden'
}

else if (obj.options[obj.selectedIndex].value == "136")
{
document.getElementById('secID1').style.visibility = 'hidden'
document.getElementById('brhID1').style.visibility = 'visible'
document.getElementById('divID1').style.visibility = 'hidden'
document.getElementById('depID1').style.visibility = 'hidden'
}
else if (obj.options[obj.selectedIndex].value == "135")
{
document.getElementById('secID1').style.visibility = 'hidden'
document.getElementById('brhID1').style.visibility = 'hidden'
document.getElementById('divID1').style.visibility = 'visible'
document.getElementById('depID1').style.visibility = 'hidden'
}
else if (obj.options[obj.selectedIndex].value == "134")
{
document.getElementById('secID1').style.visibility = 'hidden'
document.getElementById('brhID1').style.visibility = 'hidden'
document.getElementById('divID1').style.visibility = 'hidden'
document.getElementById('depID1').style.visibility = 'visible'
}
else
{
document.getElementById('secID1').style.visibility = 'hidden'
document.getElementById('brhID1').style.visibility = 'hidden'
document.getElementById('divID1').style.visibility = 'hidden'
document.getElementById('depID1').style.visibility = 'hidden'
}

}
//-->
</script>

vs

<script language="JavaScript">
<!--
function showtype(obj) {
var selectfrmvalue = obj.options[obj.selectedIndex].value
switch (selectfrmvalue)
{
case 134:
document.getElementById('secID1').style.visibility = 'hidden'
document.getElementById('brhID1').style.visibility = 'hidden'
document.getElementById('divID1').style.visibility = 'hidden'
document.getElementById('depID1').style.visibility = 'visible'
break
case 135:
document.getElementById('secID1').style.visibility = 'hidden'
document.getElementById('brhID1').style.visibility = 'hidden'
document.getElementById('divID1').style.visibility = 'visible'
document.getElementById('depID1').style.visibility = 'hidden'
break
case 136:
document.getElementById('secID1').style.visibility = 'hidden'
document.getElementById('brhID1').style.visibility = 'visible'
document.getElementById('divID1').style.visibility = 'hidden'
document.getElementById('depID1').style.visibility = 'hidden'
break
case 137:
document.getElementById('secID1').style.visibility = 'visible'
document.getElementById('brhID1').style.visibility = 'hidden'
document.getElementById('divID1').style.visibility = 'hidden'
document.getElementById('depID1').style.visibility = 'hidden'
break
default:
document.getElementById('secID1').style.visibility = 'hidden'
document.getElementById('brhID1').style.visibility = 'hidden'
document.getElementById('divID1').style.visibility = 'hidden'
document.getElementById('depID1').style.visibility = 'hidden'
}
}
//-->
</script>
Jul 20 '05 #1
7 1521
On 23 Feb 2004 08:26:33 -0800, reneecccwest <re**********@hotmail.com>
wrote:
Hello, Aren't they same conditional statment to show the resut?
but somehow "switch" condtional stmt always gets the default value when
I excute?
what did I wrong?
In the switch statement, you're comparing a string, selectfrmvalue, to
numbers. For example,

switch (selectfrmvalue)
{
case 134:
...

should be

switch (selectfrmvalue)
{
case '134':
...
<script language="JavaScript">
This should read

<script type="text/javascript">

The type attribute is required and is sufficient. The language attribute
is deprecated.
<!--
The practice of script hiding is obsolete. Remove SGML comment delimiters
from script blocks.
function showtype(obj) {
if (obj.options[obj.selectedIndex].value == "137")


You really should store that value in a local variable like you do in the
switch version. Not only is it faster, it's fewer bytes to download. You
could do same with the four identical getElementById() calls in each
branch: perform them once at the start, then alter the attributes.

Finally, you should terminate every statement with a semi-colon (;). It's
good practice.

[snip]

Hope that helps,
Mike

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 20 '05 #2
I tried, but it does not seem to be working.
did I do wrong again?

<script type="text/javascript">
function showtype(obj) {
var selectfrmvalue = parseInt(obj.options[obj.selectedIndex].value)
var getEsec = document.getElementById('secID1').style.visibility
var getEbrh = document.getElementById('brhID1').style.visibility
var getEdiv = document.getElementById('divID1').style.visibility
var getEdep = document.getElementById('depID1').style.visibility
switch (selectfrmvalue)
{
case 137:
getEsec = 'visible';
getEbrh = 'hidden';
getEdiv = 'hidden';
getEdep = 'hidden';
break
case 136:
getEsec = 'hidden';
getEbrh = 'visible';
getEdiv = 'hidden';
getEdep = 'hidden';
break
case 135:
getEsec = 'hidden';
getEbrh = 'hidden';
getEdiv = 'visible';
getEdep = 'hidden';
break
case 134:
getEsecy = 'hidden';
getEbrh = 'hidden';
getEdiv = 'hidden';
getEdep = 'visible';
break
default:
getEsec = 'hidden';
getEbrh = 'hidden';
getEdiv = 'hidden';
getEdep = 'hidden';
}
}
</script>
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #3
On 23 Feb 2004 17:39:12 GMT, reneeccc west <re**********@hotmail.com>
wrote:
I tried, but it does not seem to be working.
did I do wrong again?

<script type="text/javascript">
function showtype(obj) {
var selectfrmvalue = parseInt(obj.options[obj.selectedIndex].value)
var getEsec = document.getElementById('secID1').style.visibility
var getEbrh = document.getElementById('brhID1').style.visibility
var getEdiv = document.getElementById('divID1').style.visibility
var getEdep = document.getElementById('depID1').style.visibility


The "getE___" variables above will be primitives, with the same type as
Style.visibility. When you modify them, the changes won't be reflected in
the HTML document. What you need are references.

Try:

var getEsec = document.getElementById('secID1').style;

then alter the document with:

getEsec.visibility = 'visible';

If there are further problems, do ask.

Mike

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 20 '05 #4
thank you, it works, but it is not clear to me why second one won't be
reflected in the HTML document.

var getEsec = document.getElementById('secID1').style;

vs

var getEsec = document.getElementById('secID1').style.visibility ;

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #5
reneeccc west <re**********@hotmail.com> writes:
thank you, it works, but it is not clear to me why second one won't be
reflected in the HTML document.

var getEsec = document.getElementById('secID1').style;
This assigns an object to getEsec, the style object associated with
the element. You can change that object's properties, and the it will
be reflected in how the element looks, because the connection between
element and style object is not changed.
vs

var getEsec = document.getElementById('secID1').style.visibility ;


This will assign a string to getEsec, a simple value. You can change
the value of the getEsec variable, but it won't be reflected in the
style object, and therefore not at all in how the element looks.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #6
On 23 Feb 2004 18:48:35 GMT, reneeccc west <re**********@hotmail.com>
wrote:
thank you, it works, but it is not clear to me why second one won't be
reflected in the HTML document.

var getEsec = document.getElementById('secID1').style;

vs

var getEsec = document.getElementById('secID1').style.visibility ;


The types string, boolean and number are primitive types. When they are
assigned to variables, the value is copied. Changes to that variable
affect that variable only. The property, visibility, is a string (a
primitive).

The types array and object are composite types. When they are assigned to
variables, a reference to that entity is assigned; no copy is made.
Because the variable points to the original entity, that entity is
altered. The property, style, is an object.

Consider this example:

var a = 'text';
var x = new Object;
x.t = 'text;

var b = a;
var y = x;

alert( 'a: ' + a + '\tb: ' + b + '\n' + 'x.t: ' + x.t + '\ty.t: ' + y.t
);
b = 'new text';
y.t = 'new text';
alert( 'a: ' + a + '\t\tb: ' + b + '\n' + 'x.t: ' + x.t + '\ty.t: '
+ y.t );

In the first alert box, you should see:

a: text b: text
x.t: text y.t: text

In the second:

a: text b: new text
x.t: new text y.t: new text

Even though 'y' was the one that was modified, 'x' was also changed. That
is because both 'x' and 'y' refer to the same object. 'a' kept its
original value because 'b' contained a separate copy of the value of 'a'.

Does that help?

Mike

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 20 '05 #7
now i got it. Thank you for your clear explanation.
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #8

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

Similar topics

14
by: Rudi Hansen | last post by:
I dont seem to be able to find the switch statement in Python. I would like to be able to do switch(var) case 1 : print "var = 1" case 2: print "var = 2"
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...
5
by: Bryan Parkoff | last post by:
C++ programmers and I tried to experience by writing 65536 switch cases. It is too large for Windows XP and other operating system to handle. It looks like that JMP Table obtains 256K bytes for...
10
by: clueless_google | last post by:
hello. i've been beating my head against a wall over this for too long. setting the variables 'z' or 'y' to differing numbers, the following 'if/else' code snippet works fine; however, the ...
65
by: He Shiming | last post by:
Hi, I just wrote a function that has over 200 "cases" wrapped in a "switch" statement. I'm wondering if there are performance issues in such implementation. Do I need to optimize it some way? ...
3
by: pgraeve | last post by:
I am a convert from VB to C# so bear with me on this "conversion" question C# switch statement seems to be the closest relative to VB's Select Case. I used VB's Select Case statement liberally. ...
11
by: ME | last post by:
In C# the following code generates a compiler error ("A constant value is expected"): public void Test(string value) { switch (value) { case SimpleEnum.One.ToString(): MessageBox.Show("Test...
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;
7
by: Rohit | last post by:
Hi, I am working on a switch module which after reading voltage through a port pin and caterogizing it into three ranges(open,low or high), passes this range to a function switch_status() with...
11
by: =?Utf-8?B?anAybXNmdA==?= | last post by:
Can switch statements be nested? I've got a large routine that runs off of a switch statement. If one of the switches in switch #1 is true, that enters switch statement #2. Some of the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
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...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.