472,337 Members | 1,449 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,337 software developers and data experts.

simple question about radio buttons

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
5 2984
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Ken Fine | last post by:
I've made a content managment system that uses icons to represent page layouts. To choose a different layout, the user clicks on a radio button...
2
by: Jeff | last post by:
I'm trying to create a dynamic form that can have multiple groups of radio buttons (each group has two buttons) with the same name. Essentially,...
4
by: Oscar Monteiro | last post by:
I Have to sets of Radio buttons like so: <input type="radio" name=p1 value=1> <input type="radio" name=p1 value=2> <input type="radio" name=p1...
2
by: James P. | last post by:
Help, I need to display radio buttons on a form. The data is from SQL table: each row in each table is displayed as a radio button. I have...
24
by: firstcustomer | last post by:
Hi, Firstly, I know NOTHING about Javascript I'm afraid, so I'm hoping that someone will be able to point me to a ready-made solution to my...
1
by: kenny8787 | last post by:
Hi, can anyone help here? I have the following code generated from a database, I want to have javascript calculate the costs of the selected...
10
by: IchBin | last post by:
I am trying to set the state of a radio button. I do not see what I am doing wrong. Sorry, I am new at this.. I need another set of eyes to look at...
2
by: dpazza | last post by:
Hi, I'm creating a quiz on using a form in VB 2005 express. I have four sets of questions and answers (labels and radio buttons) and I change...
4
by: Blasting Cap | last post by:
I have a page that has a number of radio buttons that will be displayed to different access levels of a user who logs in to my website. For...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...

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.