Select radio buttons automatically when input text field is clicked

Discussion in 'Javascript' started by namanhvu@gmail.com, Sep 20, 2006.

  1. Guest

    Hi everyone,

    I'm trying to create a form where the radio button is automatically
    selected when the input text field beside it is clicked. I know I need
    to use "onClick" somewhere but I don't know how.

    With links I found it it would work like this:

    <SCRIPT LANGUAGE="JavaScript">
    function click(which) {
    document.theForm.theRadio[which].checked = true;
    }
    </SCRIPT>

    <FORM NAME="theForm">
    <INPUT TYPE="RADIO" NAME="theRadio" CHECKED> <A
    HREF="javascript:click(0)">Click this</A>
    <BR>
    <INPUT TYPE="RADIO" NAME="theRadio"> <A
    HREF="javascript:click(1)">Click this</A>
    </FORM>

    However, with input fields, it didn't work when I tried this:

    <FORM NAME="theForm">
    <INPUT TYPE="RADIO" NAME="theRadio" > Button A <input type="text"
    name="blah2" onClick="javascript:click(0)">
    <BR>
    <INPUT TYPE="RADIO" NAME="theRadio"> Button B <input type="text"
    name="blah2" onClick="javascript:click(1)">
    </FORM>

    Can anyone help me please?

    TIA.
     
    , Sep 20, 2006
    #1
    1. Advertising

  2. wrote:


    > function click(which) {
    > document.theForm.theRadio[which].checked = true;
    > }



    > <FORM NAME="theForm">
    > <INPUT TYPE="RADIO" NAME="theRadio" > Button A <input type="text"
    > name="blah2" onClick="javascript:click(0)">


    That has the effect of calling the function named 'click' associated
    with the clicked element, instead of your function with the same name.
    Rename your function to something else like say fClick, then call it
    thus:

    onClick="fClick(0)"
     
    Stephen Chalmers, Sep 20, 2006
    #2
    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. Scott D Johnson
    Replies:
    1
    Views:
    1,135
    Scott D Johnson
    Sep 27, 2006
  2. djjohnst
    Replies:
    0
    Views:
    369
    djjohnst
    Jun 6, 2007
  3. mitch-co2
    Replies:
    2
    Views:
    298
    mitch-co2
    Feb 22, 2005
  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