simple question about radio buttons

Discussion in 'Javascript' started by Digital Puer, Jul 11, 2005.

  1. Digital Puer

    Digital Puer Guest

    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.
     
    Digital Puer, Jul 11, 2005
    #1
    1. Advertising

  2. Digital Puer

    web.dev Guest

    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. :)
     
    web.dev, Jul 11, 2005
    #2
    1. Advertising

  3. Digital Puer

    ASM Guest

    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
     
    ASM, Jul 11, 2005
    #3
  4. Digital Puer

    RobG Guest

    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
     
    RobG, Jul 12, 2005
    #4
  5. Digital Puer

    Digital Puer Guest

    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>
     
    Digital Puer, Jul 12, 2005
    #5
  6. Digital Puer

    RobG Guest

    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
     
    RobG, Jul 13, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian Henry

    radio buttons on form grouping question

    Brian Henry, Nov 20, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    5,117
    Jon Paugh
    Nov 20, 2003
  2. Scott D Johnson
    Replies:
    1
    Views:
    1,135
    Scott D Johnson
    Sep 27, 2006
  3. Jonathan N. Little
    Replies:
    3
    Views:
    547
    Andy Dingley
    Apr 2, 2007
  4. Dr. Leff
    Replies:
    3
    Views:
    331
    David Mark
    Oct 15, 2007
  5. Dr. Leff
    Replies:
    3
    Views:
    254
    David Mark
    Oct 15, 2007
Loading...

Share This Page