SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Zealot akohl's Avatar
    Join Date
    May 2001
    Location
    Israel
    Posts
    184
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    switch structure doesn't work!

    In my page, one variable containing a string value is matching two different cases. The cases don't really match and so I figure I did something wrong setting up the switch structure.



    Instead of pasting it here, I thought it would be easier to send you to the site where some alerts are already in place to update you on the debugging proccess so far and clarify the situation.

    So if you want to try to help, please point ie5 and up to http://www.akohl.com/simpsons/

    You will find a personality test. Choose answer "a" for both questions 1 and 3 and you'll see the alerts that show that one expression matches two cases.

    Thanks

  2. #2
    SitePoint Guru Vincent Puglia's Avatar
    Join Date
    Feb 2000
    Location
    where the World once stood
    Posts
    700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Andy,

    The following should be dealt with first:

    1) in the button's onClick, change it so you send the form object:

    ....onclick= "scoreIt(this.form)" ...

    this will enable you to do the following

    2) change the function declaration to accept the form object as a parameter:

    function scoreIt(formObj)


    3) you can then...

    for(i = 0; i < formObj.length-1; i++)
    {

    4) note the "i = 0" instead of 'i = 1' -- javascript begins array counting with 0, not 1. This will enable the next line:

    5)
    if( (formObj.elements[i].name != formObj.elements[i-1].name ) || ( i==0) )

    note the parens. within a conditional, each statement should be enclosed with parens, as well as the entire conditional.

    6) I would also enclose each case with braces and a break, as in:

    case 'milk':
    {
    switch .....
    {
    }
    break;
    }
    case 'tv':
    ....

    Vinny
    Where the World Once Stood
    the blades of grass
    cut me still

  3. #3
    SitePoint Zealot akohl's Avatar
    Join Date
    May 2001
    Location
    Israel
    Posts
    184
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hey Vinny (cool name)

    I will try your squigely bracets suggestion. But its strange because other switches are working in the code without them.

    I don't really see the point of your other suggestions though. Although they do make the code simpler, which is a nice thing I suppose. But do they have to do with the problem of the switches not working?

    I s there a logical error in my code as opposed to a syntax one?

    Andy

  4. #4
    SitePoint Zealot akohl's Avatar
    Join Date
    May 2001
    Location
    Israel
    Posts
    184
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    works now. Now, to the other browsers.

    I just replaced the switch case structure with an if, else if, ect structure.

    I wonder why it didn't work before.

    Now, to get it to work in other browsers.

    How could I script the exact same functions for netscape and opera?

  5. #5
    What? Maelstrom's Avatar
    Join Date
    Oct 2001
    Location
    Whistler BC originally from Guelph Ontario
    Posts
    2,175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by akohl
    Hey Vinny (cool name)

    I will try your squigely bracets suggestion. But its strange because other switches are working in the code without them.

    I don't really see the point of your other suggestions though. Although they do make the code simpler, which is a nice thing I suppose. But do they have to do with the problem of the switches not working?

    I s there a logical error in my code as opposed to a syntax one?

    Andy
    His other suggestions produce a more resusable 'clean' code. When programming one should always try to create that look and feel of good clean readable reusable form within their structure. There is no inherent error within your code but Vinny's is better

    The if else structure isn't working in other browsers?. What isn't working in other browsers I am sorry I don't quite get your last question?
    Maelstrom Personal - Apparition Visions
    Development - PhP || Mysql || Zend || Devshed
    Unix - FreeBSD || FreeBsdForums || Man Pages
    They made me a sitepoint Mentor - Feel free to PM me or Email me and I will see if I can help.

  6. #6
    SitePoint Zealot akohl's Avatar
    Join Date
    May 2001
    Location
    Israel
    Posts
    184
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    other browsers

    Nothing in the script works in other browsers. That is to say that nothing happens when you press the button in other browsers. I tested it with netscape 4 and Opera 6.

    I wrote the script with the syntax that I am used to, which is for ie. How do I go about converting this to a cross browser script? Are there some handy tricks you could give me?

  7. #7
    SitePoint Guru Vincent Puglia's Avatar
    Join Date
    Feb 2000
    Location
    where the World once stood
    Posts
    700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Andy,

    Re #5 above:

    this:
    if( (formObj.elements[i].name != ....

    is not the same as:

    if( (formObj.elements(i).name !=

    Syntax, in any language, is important! It's what we all spent our youth trying to learn.

    Try implementing my suggestions and cleaning up the syntax. Once that is done, the logic can be looked at.

    Vinny
    Where the World Once Stood
    the blades of grass
    cut me still

  8. #8
    SitePoint Zealot akohl's Avatar
    Join Date
    May 2001
    Location
    Israel
    Posts
    184
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Vincent Puglia
    Hi Andy,

    Re #5 above:

    this:
    if( (formObj.elements[i].name != ....

    is not the same as:

    if( (formObj.elements(i).name !=

    Vinny
    That's right. With the syntax I used, I am referencing the elements collection. Whereas you are referencing an array. Does the elements[i] array exitst? Is it part of my internet explorer grounding that I assumed that the correct way to reference the radio buttons was through the elements collection?

    While you try to figure out my confusion here, I'll look at your suggestions again, although its not so urgent at this stage since the script works. In other words, the logical problem is not there anymore, just the crossbrowser one.

  9. #9
    What? Maelstrom's Avatar
    Join Date
    Oct 2001
    Location
    Whistler BC originally from Guelph Ontario
    Posts
    2,175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To be honest all of the form elements are contained within an array. But yes to be more direct the way to access radio buttons is through and array reference
    Maelstrom Personal - Apparition Visions
    Development - PhP || Mysql || Zend || Devshed
    Unix - FreeBSD || FreeBsdForums || Man Pages
    They made me a sitepoint Mentor - Feel free to PM me or Email me and I will see if I can help.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •