By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,046 Members | 2,105 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,046 IT Pros & Developers. It's quick & easy.

simple question about radio buttons

P: n/a
I have the following HTML form:

- radio button A (default selected)
- radio button B
- input field, of type "file" with "Choose" button
- submit button

I would like to have it so that if the user clicks on the
"Choose" button of the input field (to select a file on
the local disk), then radio button B is automatically
selected. Can someone tell me how to do that? I'm a
complete Javascript newbie. Thanks.

Jul 23 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Hi Digital,

Here is the javascript section that you can place between the <head>
elements.

<script language = "javascript" type = "text/javascript">
function optionC(elm)
{
elm.form.choice[1].checked = true;

return true;
}
</script>

And here is the html form that I've used to test:

<form action = "url" method = "post">
<input type = "radio" name = "choice" checked = "checked" />Option
A <br/>
<input type = "radio" name = "choice"/>Option B <br/>
<input type = "file" value = "Get File" onClick = "return
optionC(this)" /><br/>
<input type = "submit" value = "Submit"/>
</form>

One thing to note, depending on what you're trying to do, in the
function optionC, if you return true, it will bring up the 'Choose
File' dialog box, while returning false will not.

Hope this helps. :)

Jul 23 '05 #2

P: n/a
ASM
Digital Puer wrote:
I have the following HTML form:

- radio button A (default selected)
- radio button B
- input field, of type "file" with "Choose" button
- submit button

I would like to have it so that if the user clicks on the
"Choose" button of the input field (to select a file on
the local disk), then radio button B is automatically
selected.


I think you can only do that if a file has been selected

A <input type=radio name="radiosBt" value="of">
B <input type=radio name="radiosBt" value="on">
<input type=file onchange="radiosBut[1].checked=true">

--
Stephane Moriaux et son [moins] vieux Mac
Jul 23 '05 #3

P: n/a
Digital Puer wrote:
I have the following HTML form:

- radio button A (default selected)
- radio button B
- input field, of type "file" with "Choose" button
- submit button

I would like to have it so that if the user clicks on the
"Choose" button of the input field (to select a file on
the local disk), then radio button B is automatically
selected. Can someone tell me how to do that? I'm a
complete Javascript newbie. Thanks.


Neither of the above suggestions will provide a button that reflects the
state of the file input. Below is a suggested strategy:

If JavaScript is disabled, you can't reliably say whether a file has
been selected or not, so don't show the radio buttons.

Change the selected radio button based on the actual state of the file
select. Check the status whenever some action happens on any of the 3
form controls involved and set the appropriate button to checked.

The following script runs whenever the radio buttons are checked or the
value of the file input is changed. The checkboxes also reflect the
value of the input if the user manually clears the input, the page is
re-loaded or the reset button clicked. The radio buttons are only shown
if JavaScript is available and appropriate features supported courtesy
of an onload function.

It still has a problem that the user may want to change the value of the
radio button (but they can't). And if the user changes the availability
of JavaScript after loading the page, all bets are off!

It is just a demo to show the lengths you must go to to support
something which seems quite simple but requires significant effort to
make worthwhile. Note also that browser behaviour differs on page
re-load - some will clear the file input, others won't, so the onload
must check the status of the file input (or just reset the form).

In the head:

<style type="text/css">
..Vis {display: inline;}
..NVis {display: none;}
</style>

<script type="text/javascript">
function checkMe( el ){
var f = el.form;
if ( '' != f.fileSelect.value ) {
f.rBut[1].checked = true;
} else {
f.rBut[0].checked = true;
}
}
window.onload = function() {
if ( !document.getElementById && !document.body.style ){
return;
}
document.getElementById('fileAdviceJS').className = 'Vis';
document.getElementById('fileAdvice').className = 'NVis';
checkMe( document.forms['fileForm'].elements['fileSelect'] );
}

</script>
In the body:

<form name="fileForm" action="">
<span id="fileAdviceJS" class="NVis">File selected?
<input type="radio" value="n" name="rBut" checked
onchange=" checkMe(this);">No&nbsp;<input
type="radio" value="y" name="rBut"
onchange="checkMe(this);">Yes</span>
<span id="fileAdvice" class="Vis">Select a file</span><br>
<input id="fileSelect" name="fileSelect" type="file"
onchange="checkMe(this);">
<br>
<input type="reset">
</form>


--
Rob
Jul 23 '05 #4

P: n/a
Thanks everyone for your help.

This is what I ended up doing. It seems to work well.
Is this right?
<form action="foo.jsp" enctype="multipart/form-data" method="POST">
<input type="radio" name="have_foo" value="no"
checked="checked">
No, I do not have a foo file.<br>

<input type="radio" name="have_foo" value="yes" >
Yes, I already have a foo file:

<input type="file" name="file" size="70"
onfocus="elements[1].checked=true;">

<input type="submit" value="Continue">
</form>

Jul 23 '05 #5

P: n/a
Digital Puer wrote:
Thanks everyone for your help.

This is what I ended up doing. It seems to work well.
Is this right?
If 'right' means syntactically correct and does it run to completion
without errors, then yes, it's right.

But if you want something useful, then no, it's not.


<form action="foo.jsp" enctype="multipart/form-data" method="POST">
<input type="radio" name="have_foo" value="no"
checked="checked">
No, I do not have a foo file.<br>

<input type="radio" name="have_foo" value="yes" >
Yes, I already have a foo file:

<input type="file" name="file" size="70"
onfocus="elements[1].checked=true;">

<input type="submit" value="Continue">
</form>


If I click on 'browse', then cancel, your form tells me I've already
selected a file when I haven't. If I have JavaScript disabled, I'm told
I haven't selected a file regardless of whether I have or not. If I
select a file, then delete the contents of the input, I'm told I've
selected a file but if I submit the form, nothing will be sent.

In IE I can select a file, use the back button, then forward and the
input is empty but the radio says I've already selected a file. I can
just select 'No...' or 'Yes...' anytime I like, regardless of whether
I've chosen a file or not.

As a user, am I supposed to select the correct radio button? Does it
matter? What if I submit the form after selecting a file but the
'No...' radio is checked? Or vice versa - if I select a file, then
delete the contents of the input, the 'Yes...' radio is still selected -
will the file still be sent? Does selecting the 'No...' radio cancel
the send? ...

Users *will* ask all the above questions (and many more that neither you
or I have thought of).

The radio button 'works' in a few of of many possible scenarios and will
fail for all the rest - users will begin to distrust the form and wonder
whether other features are as dysfunctional.

Forms must be held to a higher standard of quality that other UI
components. Every feature of your form must be utterly bulletproof and
provide some valuable functionality for your visitor - server processes
must be able to deal with any and all possible client-side outcomes.

If selection of a file is required, validate the form when it is
submitted and validate again on the server expecting that no validation
at all occurred at the client.

However, you are on the right track by providing non-intrusive prompts -
messages that use alert dialogs annoy the hell out of people.
--
Rob
Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.