Syntax problem

Discussion in 'Javascript' started by Claes Andersen, Oct 24, 2003.

  1. Hi,

    I got this piece of code that i intend to use for a quiz program. What it
    does is the following. When a user makes a choice in a family of radio
    buttons, the other choices are automatically disabled, hence allowing a
    "first answer counts" system. It works fine with just one family on a page,
    but I need it to work with more ... The function is called from the
    respective radio buttons like this:

    <input onclick="lockChoice(1,0);" type="radio" name="a1" value="1">

    The lockChoice variables (1,0) in the above example are used (or intended to
    be so - I do after all have a problem that i need help for;-) to move the
    number of the question (1) and the number of the choice (0)

    <script language="JavaScript">

    function lockChoice(answ,choice){
    for (i = 0; i <= 7; i++)
    {
    if (i != choice)
    document.test.a1.disabled = true;
    }
    </script>

    As it is above it is fine, but just for one family of radio buttons. I'm no
    shark at JavaScript, but invented the above myself, so at least i got a bit
    right ... so here is the big Q:

    How do I build the following line:

    document.test.a1.disabled = true;

    so that it is dynamic also related to answ variable passed from each of the
    choices? (a1, a2, a3, a4 etc.)? In short that the above a1 gets a bit
    like [answ][a1] or however the syntax should be.

    I tried more combinations, but im stuck:-( I'm soon getting my evening
    spoiled here, so I address the honorable audience with a hope of a fast
    solution. Any suggestions - that lead to the solve of the above - are dearly
    appriciated

    Best regards,

    Claes Andersen
     
    Claes Andersen, Oct 24, 2003
    #1
    1. Advertising

  2. Claes Andersen

    Oz Guest

    Wow, you're making this task harder than you need to. The solution is simple
    if you are aware of the getElementsByName method on the document object.
    Also you should know that your example is a missuse of a radio button. All
    related radios should have the same name. That way the browser will
    automatically automatically ensure that only one radio is selected for a
    given group.

    Here is an example implementation:

    <script language="javascript">

    function disableGroup(control){
    //create a collection of radio inputs
    var col = document.getElementsByName(control.name);
    //loop and disable all except the one that is selected
    for (var i=0;i<col.length;i++) {
    if (!col.checked)
    col.disabled = true;
    }
    }

    </script>

    <p>
    <input name="a1" value=1 type="radio" onclick="disableGroup(this)">
    <input name="a1" value=2 type="radio" onclick="disableGroup(this)">
    </p>
    <p>
    <input name="a2" value=1 type="radio" onclick="disableGroup(this)">
    <input name="a2" value=2 type="radio" onclick="disableGroup(this)">
    </p>





    "Claes Andersen" <> wrote in message
    news:3f998551$0$54787$...
    > Hi,
    >
    > I got this piece of code that i intend to use for a quiz program. What it
    > does is the following. When a user makes a choice in a family of radio
    > buttons, the other choices are automatically disabled, hence allowing a
    > "first answer counts" system. It works fine with just one family on a

    page,
    > but I need it to work with more ... The function is called from the
    > respective radio buttons like this:
    >
    > <input onclick="lockChoice(1,0);" type="radio" name="a1" value="1">
    >
    > The lockChoice variables (1,0) in the above example are used (or intended

    to
    > be so - I do after all have a problem that i need help for;-) to move the
    > number of the question (1) and the number of the choice (0)
    >
    > <script language="JavaScript">
    >
    > function lockChoice(answ,choice){
    > for (i = 0; i <= 7; i++)
    > {
    > if (i != choice)
    > document.test.a1.disabled = true;
    > }
    > </script>
    >
    > As it is above it is fine, but just for one family of radio buttons. I'm

    no
    > shark at JavaScript, but invented the above myself, so at least i got a

    bit
    > right ... so here is the big Q:
    >
    > How do I build the following line:
    >
    > document.test.a1.disabled = true;
    >
    > so that it is dynamic also related to answ variable passed from each of

    the
    > choices? (a1, a2, a3, a4 etc.)? In short that the above a1 gets a bit
    > like [answ][a1] or however the syntax should be.
    >
    > I tried more combinations, but im stuck:-( I'm soon getting my evening
    > spoiled here, so I address the honorable audience with a hope of a fast
    > solution. Any suggestions - that lead to the solve of the above - are

    dearly
    > appriciated
    >
    > Best regards,
    >
    > Claes Andersen
    >
    >
     
    Oz, Oct 24, 2003
    #2
    1. Advertising

  3. Loads of thanks goes to Oz, who came up with a fast and good solution to my
    problem.

    Claes
     
    Claes Andersen, Oct 24, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. gabriele renzi
    Replies:
    2
    Views:
    204
    gabriele renzi
    Dec 31, 2005
  2. Ken Bloom
    Replies:
    3
    Views:
    213
  3. Good Night Moon
    Replies:
    9
    Views:
    283
    Rick DeNatale
    Jul 25, 2007
  4. Jacob Grover
    Replies:
    5
    Views:
    318
    Jacob Grover
    Jul 18, 2008
  5. Mark Richards
    Replies:
    3
    Views:
    316
    Tad McClellan
    Nov 18, 2007
Loading...

Share This Page