Results 1 to 7 of 7
  1. #1
    5 Star Lounger
    Join Date
    Jan 2001
    Location
    austin, Texas, USA
    Posts
    1,029
    Thanks
    0
    Thanked 0 Times in 0 Posts

    javascript SWITCH problem

    javascript! i does hates it...

    I am trying to assemble a SWITCH statement and am having no luck getting it to work. The de###### (which is really aweful) doesn't shed much light, but I think the problem is getting the SWITCH to evaluate the variable I am trying to pass to it.

    here's the skinny:

    on the form, there is a 'reset row' button with the onclick set to:
    <font face="Georgia">onclick="java script:ResetRow('BasicNeeds')"</font face=georgia>

    in the javascript I am trying to do something that looks like this:

    <font face="Georgia">function ResetRow(RowName)
    {

    switch (RowName)
    {
    Case "BasicNeeds":
    for (i=0; i<3; i++)
    document.FollowUpForm.BasicNeeds[ i ].checked = false;
    document.FollowUpForm.BasicNeeds_Other.checked = false;
    break;

    Case 'MentalHealth':
    for (i=0; i<3; i++)
    document.FollowUpForm.MentalHealth[ i ].checked = false;
    document.FollowUpForm.MentalHealth_Other.checked = false;
    break;

    default: Alert('Boo');
    }

    }
    </font face=georgia>

    As you can see I am attempting to load the var RowName into the each case on the Switch construction, but I am not meeting with success. Any ideas?

    Also, I'd rather not even use a SWITCH construction, just something like this:

    <font face="Georgia"> for (i=0; i<3; i++)
    document.FollowUpForm.RowName[ i ].checked = false;
    document.FollowUpForm.RowName+'_Other'+.checked = false;
    </font face=georgia>

    but I can't seem to get the var to load into the DOM string either.

    Any ideas?

    <img src=/S/compute.gif border=0 alt=compute width=40 height=20>

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: javascript SWITCH problem

    Does <code>Case</code> need to be <code>case</code> (lower case)?
    Do you need { } around multiple statements inside a <code>for</code>?

  3. #3
    5 Star Lounger
    Join Date
    Jan 2001
    Location
    austin, Texas, USA
    Posts
    1,029
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: javascript SWITCH problem

    i don't know if case needs to be lowercase -- i'll try that out.

    you don't need braces in the for loop.

  4. #4
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: javascript SWITCH problem

    The way to refer to an element in a document is by its id attribute, an attribute that must be unique.

    For example, <tr id="BasicNeeds"><td>...</td></tr>

    In your code:

    function doStuff(strRowID){
    var rowTarget = document.getElementById(strRowID);

    There are DOM methods to iterate through the "child nodes" of your target row. However, that's a little complicated for a quick Lounge post.

    Since you really want to target a few controls with a common prefix, there is an easier way. This presupposes that you ID them as BasicNeedsChk0, etc. (for convenience, make the ID and NAME the same unique value). Then your code could be more like this (not tested):

    function doStuff(strRowID){
    for (i=0; i<3; i++) { document.getElementById(strRowID+"_Chk"+i).checked = false; }
    document.getElementById(strRowID+"_Other").checked = false;

  5. #5
    5 Star Lounger
    Join Date
    Jan 2001
    Location
    austin, Texas, USA
    Posts
    1,029
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: javascript SWITCH problem

    hmmm... this makes some sense, but is a little confusing too. What i have is a set of radio buttons; the javascript is there to allow the user to clear a row if they accidentally clicked one of the options. As you know, radio buttons have to share the same name to comprise a set. so I have this sort of thing:

    <p align="center"><input type="radio" name="BasicNeeds" value="All Services Provided"></td>
    <td width="20%" height="40">
    <p align="center"><input type="radio" name="BasicNeeds" value="Some Services Provided"></td>
    <td width="20%" height="40">
    <p align="center"><input type="radio" name="BasicNeeds" value="No Services Provided"></td>
    <td width="20%" height="40">
    <p align="center"><input type="radio" name="BasicNeeds_Other" value="New Services Identified"></td>

    which creates a three-button set BasicNeeds and a stand-alone 'Other' (which, now that I look at it, should really be a checkbox...) -- so you're saying I need to assign a unique ID to the 3-button set? Or each button should have it's own id? Or just plop an id="BasicNeeds" so the javascript knows what to do?

    BTW, I can get the javascript to clear the set as it stands if I write the code outside of my brain-damaged SWITCH code, so this ID business is unexpected. However, if I can get the ref to my buttons in a single line that just builds the ref to the button group via a var, there's no need to use SWITCH in the first place, so your suggestion is great as it appears to be on the right track.

  6. #6
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: javascript SWITCH problem

    Well, there are shortcuts, but you might was well use the official DOM way as long as you are rebuilding. If you surround your button set with the <div id="data"> and </div> tags, you could use a "DOM tree walker" similar to the code I posted over here (similar but much simpler because you don't need to do the search part): Javascript - Firefox Skips a match - MozillaZine Forums. Sorry I don't have time at the moment to work on a version customized for your need.

  7. #7
    5 Star Lounger
    Join Date
    Jan 2001
    Location
    austin, Texas, USA
    Posts
    1,029
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: javascript SWITCH problem

    thanks for the tip. i was able to use your 'psuedo' code with complete and utter success. now to work on World Domination....

Posting Permissions

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