Autoselect in a radio

H

hygum

I have combination of radio and select/input:
http://sneleopard.dk/radio.html

Is there a javascript which automatically sets radio 1, when the user
does something with field 1, and automatically sets radio 2, when the
user does something with field 2?
 
E

Erwin Moller

hygum said:
I have combination of radio and select/input:
http://sneleopard.dk/radio.html

Is there a javascript which automatically sets radio 1, when the user
does something with field 1, and automatically sets radio 2, when the
user does something with field 2?

Yes, but it a question like: "Do you have a car that can drive from
Amsterdam to Paris?"
It is very basic in javascript, but you will not find 'a javascript' that
will work at once in your current setup.

I know I am not very helpfull untill now. :)
What you need to do/study:
- naming formelements
- onChange eventhandler to detect a change in some field.
- how to adres a radiobuttongroup
something like:
document.forms['yourformname'].radiogroupname[0].checked = true;

Good luck.

Regards,
Erwin Moller
 
R

RobG

hygum said:
I have combination of radio and select/input:
http://sneleopard.dk/radio.html

Is there a javascript which automatically sets radio 1, when the user
does something with field 1, and automatically sets radio 2, when the
user does something with field 2?

You need to clearly explain your requirements. One radio button should
always be selected, which one should be selected by default?

Should the selected button represent the last field that was changed?
Or does button 1 get selected if field 1 changes and button 2 if field 2
changes? Should both be selected at the same time in this case? If so,
then they probably should be checkboxes not radio buttons.

Further help is dependant on getting more information.
 
H

hygum

I'm not so stupid as I sound!

The default should be radio 2.

No it's not checkboxes I want. The radiocheck shall change following
the changes in the fields. So it is what Rob suggests: button 1 get
selected if field 1 changes and button 2 if field 2 changes.
 
B

Bangalore Info

TRY LIKE THIS ....

Cheers
Ajay.

****************************************************************************
******************
<HEAD>

<SCRIPT LANGUAGE="JavaScript">

function changeBox(cbox) {
cbox.checked = true;
}

</script>

</HEAD>

<BODY>

<form name=demoform>
<input type=radio name=agreebox1>
<a href="" onClick="changeBox(document.demoform.agreebox1);return
false">Clicking this text also checks the radio to the left.</a><input
type=radio name=agreebox2>
<a href="" onClick="changeBox(document.demoform.agreebox2);return
false">Clicking this text also checks the radio to the left.</a>
</form>


****************************************************************************
******************
 
R

RobG

hygum said:
I have give it a try, but it doesn't work for me, whats wrong:
http://sneleopard.dk/radio.htm

You shouldn't use an A element this way, but for the sake of the example...

<a href="#" onClick="
changeBox('document.demoform.agreebox1.selected');
return false;
">...</a>

For your form, you likely want to use onchange rather than onclick.

You can also use this.form rather than document.demoform if you do as
per your initial original example and put the onchange on a select
element in the same form.
 
R

Robi

hygum wrote in message news:[email protected]...
I have give it a try, but it doesn't work for me, whats wrong:
http://sneleopard.dk/radio.htm

I know this is the Javascript NG, but why don't you use the <label> tag instead?

<form name=demoform>
<label>
<input type=radio name=agreebox1>Clicking this text also checks the radio to the left.
</label><br>
<label>
<input type=radio name=agreebox2>Clicking this text also checks the radio to the left.
</label>
</form>
 
R

RobG

Robi said:
hygum wrote in message


I know this is the Javascript NG, but why don't you use the <label> tag instead?

Because it is unsuitable - see below (thought the OP's requirements are
pretty vague).
<form name=demoform>
<label>
<input type=radio name=agreebox1>Clicking this text also checks the radio to the left.
</label><br>
<label>
<input type=radio name=agreebox2>Clicking this text also checks the radio to the left.
</label>
</form>

The HTML spec says that in the absence of a 'for' attribute, the label
applies to the enclosed element - however IE doesn't follow the spec.
You must use the 'for' attribute and an id on the radio button.

The HTML spec is also ambiguous about how to deal with sets of radio
buttons (in this case, two sets of one button) where none are checked by
default:

<URL:http://www.w3.org/TR/html4/interact/forms.html#radio>

Browsers can make their own decision regarding which one is checked by
default - most seem to not check any. The issue then becomes how to
return the form to the original state - once one button is checked,
users can't get back to none checked without either resetting the form
or reloading the page.

It is also a good idea to always enclose attribute values in quotes,
even though they are not always needed.

<form name="demoform" action="">
<label for="agreebox1">
<input type="radio" name="agreebox" id="agreebox1">Clicking
this text also checks the radio to the left.
</label><br>
<label for="agreebox2">
<input type="radio" name="agreebox" id="agreebox2">Clicking
this text also checks the radio to the left.
</label><br>
<input type="reset">
</form>

However, the OP probably wants to make the radio checked based on the
change of another element, in which case a label is not suitable as
putting a second element inside the label creates serious usability issues:

- in Firefox the label keeps putting focus on the radio button
whenever the other elements get focus from clicks but not from keyboard
navigation.

- in IE, the other element is ignored so that clicks on it do not
check the radio button.
 
R

RobG

hygum said:
sorry i can't get it to work in IE, could you please provide the full
code?

<form name="demoform" action="">
<p>
<input type="checkbox" name="agreebox1">
<a href="#" onClick="document.demoform.agreebox1.checked = true;">
Clicking this text also checks the radio to the left.</a>
<br>
<input type="checkbox" name="agreebox2">
<a href="" onClick="document.demoform.agreebox2.checked = true;">
Clicking this text also checks the radio to the left.</a>
<br>
<input type="checkbox" name="agreebox3">
<select onchange="document.demoform.agreebox3.checked = true;">
<option>
<option>Option 1
<option>Option 1
<option>Option 1
</select>
<br>
<input type="reset">
</p>
</form>
 
H

hygum

Thank you for the code.

I'm sorry but the checkboxes should be radios, because I only want one
of them checked at a time. Of course I could insert script which
unchecks all, and then checks the specific one, but making checkboxes
behave like radios is less rational as making real radios.

the word "box" in the code stems from the fact that the original script
is in fact with checkboxes.

Could you please make the same with radios instead of checkboxes?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top