Switch statement doesn't work

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
finchamgroves
Forum Newbie
Posts: 11
Joined: Wed Oct 07, 2009 10:02 am

Switch statement doesn't work

Post by finchamgroves »

Hi
The following code worked fine in FF and IE but always produces the first option in Safari.
(The CDATA statment doesn't make any difference to the outcome.)
Any advice very, very welcome!

Code: Select all

 
<html>
<head>
<script type="text/javascript">
/* <![CDATA[ */
var my_text;
var talk_01 = 'text stuff 1';
var talk_02 = 'text stuff 2';
var talk_03 = 'text stuff 3';
function speaker(talk)
    {
    switch(talk)
        {
        case(talk=1):
        my_text = talk_01;
        break;
        case(talk=2):
        my_text = talk_02;
        break;
        case(talk=3):
        my_text = talk_03;
        break;
        }
    document.getElementById('infobox').innerHTML = my_text;
    }
/*]]>*/
</script>
</head>
<body>
    <table width="85%" align="center" class="myTable1" summary="speaker table">
              <tr>
                <th><div align="left">Date</div></th>
                <th><div align="left">Speaker</div></th>
                <th><div align="left">Topic</div></th>
                <th>&nbsp;</th>
              </tr>
              <tr class='hilite'>
                <td>January 28th</td>
                <td>Aristotle</td>
                <td>Aardvarks</td>
                <td  onclick="speaker(1)">more ... </td>
              </tr>
              <tr class='hilite'>
                <td>February 25th</td>
                <td>Benjamin</td>
                <td>Butterflies</td>
                <td onclick="speaker(2)">more ... </td>
              </tr>
              <tr class='hilite'>
                <td>March 25th</td>
                <td>Canute</td>
                <td>Canoes</td>
                <td  onclick="speaker(3)">more ... </td>
              </tr>
      </table>
      <div id ="infobox"> 
        <p>
            Click <strong><em>more</em></strong>... in the above Table for more info about the talks. </p>
        <p>(If you use Internet Explorer you may need to click on a script warning at the top of your screen) </p>
      </div>
</body>
</html>
 
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Switch statement doesn't work

Post by papa »

Shouldn't it be case(talk==1): ?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Switch statement doesn't work

Post by VladSun »

Why one should even compare values in a case statement ???

http://www.w3schools.com/js/js_switch.asp

http://php.net/manual/en/control-structures.switch.php
Example 1!
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Switch statement doesn't work

Post by papa »

True :)
finchamgroves
Forum Newbie
Posts: 11
Joined: Wed Oct 07, 2009 10:02 am

Re: Switch statement doesn't work

Post by finchamgroves »

No it isn't ==
But thanks because you focussed me in the correct place.
It's

Code: Select all

 
        case 1:
        my_text = talk_01;
        break;
        case 2:
etc
 
And this has got it working in all browsers.
Cheers! :D :D :D
Post Reply