473,728 Members | 1,921 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3107
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="radio sBut[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.va lue ) {
f.rBut[1].checked = true;
} else {
f.rBut[0].checked = true;
}
}
window.onload = function() {
if ( !document.getEl ementById && !document.body. style ){
return;
}
document.getEle mentById('fileA dviceJS').class Name = 'Vis';
document.getEle mentById('fileA dvice').classNa me = 'NVis';
checkMe( document.forms['fileForm'].elements['fileSelect'] );
}

</script>
In the body:

<form name="fileForm" action="">
<span id="fileAdviceJ S" class="NVis">Fi le selected?
<input type="radio" value="n" name="rBut" checked
onchange=" checkMe(this);" >No&nbsp;<inp ut
type="radio" value="y" name="rBut"
onchange="check Me(this);">Yes</span>
<span id="fileAdvice " class="Vis">Sel ect a file</span><br>
<input id="fileSelect " name="fileSelec t" type="file"
onchange="check Me(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="multip art/form-data" method="POST">
<input type="radio" name="have_foo" value="no"
checked="checke d">
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="elemen ts[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="multip art/form-data" method="POST">
<input type="radio" name="have_foo" value="no"
checked="checke d">
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="elemen ts[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
3605
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 associated with each layout icon. On click of one of the radio buttons, the form submits and a new layout is chosen. I would prefer if people can click on the icons themselves , rather than using the radio buttons.The border or background associated with the selected icon can highlight...
2
3153
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, the form allows a user to enter as many names as they want. If they need to add more, they click an "add name" button and the javascript inserts another row of input boxes. Each row should have two radio buttons to indicate sex (M F). When you have multiple text input boxes with the same...
4
3280
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 value=3> <br> <input type="radio" name=p2 value=1> <input type="radio" name=p2 value=2> <input type="radio" name=p2 value=3> then a text area and a button:
2
11965
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 multiple SQL tables so I understand I need to put them each in a GroupBox. All the examples I saw from the books or from the web show me how to add static radio buttons at design, or dynamically at run time but with fixed radio buttons (like from an array). I need to create radio buttons based...
24
6337
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 problem! A friend of mine (honest!) is wanting to have on his site, a Javascript Calculator for working out the cost of what they want, for example: 1 widget and 2 widglets = £5.00
1
6891
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 items using radio buttons, subtotal the costs and grandtotal the costs ready for the selected items to be inserted back to the database. I did something like this before with Checkboxes, but Radio button have to be named the same to maintain their groupings.
10
6100
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 this snip of code. I am trying to set the radio button with this link of code: echo 'SCRIPT language=JavaScript setCheckedValue("'.$_SESSION.'");</SCRIPT>'; //? <snip of code>
2
5891
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 between which set of questions is currently shown on the form by changing the visible state of the radio buttons and labels utilising back and next buttons. E.g. Next button makes current radio buttons and labels invisible and
4
2519
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 instance, if there are a dozen buttons, user1 will see all twelve. User2 would see buttons 1-3,10-12 and NOT any others, and so on. Since there are several buttons on the page (each one would run a report for them), is there a way to - in an onclick event for each of the buttons - to...
0
8758
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9277
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9198
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9130
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6716
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6013
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4524
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4792
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2663
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.