473,397 Members | 1,974 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,397 software developers and data experts.

Syntax problem

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[i].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[i].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[i] 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
Jul 20 '05 #1
2 1762
Oz
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[i].checked)
col[i].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" <ca@infonaut.dk> wrote in message
news:3f***********************@dread11.news.tele.d k...
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[i].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[i].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[i] 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

Jul 20 '05 #2
Loads of thanks goes to Oz, who came up with a fast and good solution to my
problem.

Claes
Jul 20 '05 #3

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

Similar topics

699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
303
by: mike420 | last post by:
In the context of LATEX, some Pythonista asked what the big successes of Lisp were. I think there were at least three *big* successes. a. orbitz.com web site uses Lisp for algorithms, etc. b....
24
by: Andrew Koenig | last post by:
PEP 315 suggests that a statement such as do: x = foo() while x != 0: bar(x) be equivalent to while True:
35
by: Moosebumps | last post by:
Does anyone here find the list comprehension syntax awkward? I like it because it is an expression rather than a series of statements, but it is a little harder to maintain it seems. e.g. you...
23
by: C. Barnes | last post by:
I vote for def f(): (body of function) This is backwards compatible (Python <= 2.3 raise SyntaxError), and looks much nicer than @. The only problem is that you can't one-line a...
16
by: George Sakkis | last post by:
I'm sure there must have been a past thread about this topic but I don't know how to find it: How about extending the "for <X> in" syntax so that X can include default arguments ? This would be very...
29
by: shank | last post by:
1) I'm getting this error: Syntax error (missing operator) in query expression on the below statement. Can I get some advice. 2) I searched ASPFAQ and came up blank. Where can find the "rules"...
5
by: NanQuan | last post by:
I'm hoping someone can help me solve this error since I am at a total loss here. Usually I don't bother posting on any forums or groups on the internet and prefer to solve stuff myself but this is...
19
by: Nicolas Fleury | last post by:
Hi everyone, I would to know what do you think of this PEP. Any comment welcomed (even about English mistakes). PEP: XXX Title: Specialization Syntax Version: $Revision: 1.10 $...
20
by: W Karas | last post by:
Would the fear factor for concepts be slightly reduced if, instead of: concept C<typename T> { typename T::S; int T::mem(); int nonmem(); };
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.