Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > simple question about radio buttons

Reply
Thread Tools

simple question about radio buttons

 
 
Digital Puer
Guest
Posts: n/a
 
      07-11-2005
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.

 
Reply With Quote
 
 
 
 
web.dev
Guest
Posts: n/a
 
      07-11-2005
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.

 
Reply With Quote
 
 
 
 
ASM
Guest
Posts: n/a
 
      07-11-2005
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
 
Reply With Quote
 
RobG
Guest
Posts: n/a
 
      07-12-2005
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
 
Reply With Quote
 
Digital Puer
Guest
Posts: n/a
 
      07-12-2005
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>

 
Reply With Quote
 
RobG
Guest
Posts: n/a
 
      07-13-2005
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Created Radio Buttons displaying as if they were conventional buttons Dr. Leff Javascript 3 10-15-2007 09:47 PM
Created Radio Buttons displaying as if they were conventional buttons Dr. Leff Javascript 3 10-15-2007 05:18 AM
The Z axis and simple menu buttons/image buttons Jonathan N. Little HTML 3 04-02-2007 09:57 AM
ASP.NET Reset Radio Button in a group of Radio Buttons Scott D Johnson ASP .Net 1 09-27-2006 10:38 PM
radio buttons on form grouping question Brian Henry ASP .Net 1 11-20-2003 07:17 PM



Advertisments