Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jun 2009
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Thumbs down How to set Radio buttons by textbox value

    I am trying to set a radio button by a textbox value and also have the radio button be able to set the textbox. So the user can either select a radio button or enter in the text. Is there a simple way to make this happen? Here is what I have so far, which sets the radio button value in the text box. I just need help setting the textbox value to the radio button. Thanks

    Code:
    <HEAD>
    <script>
    
    var number="";
    
    function getRadios(what){
    
    j=what.number.length;
    for (i=0; i<j; i++){
    if(what.number[i].checked) var number = what.number[i].value
    }
    
    what.buttons.value = (number)
    }
    
    </script>
    </HEAD>
    
    
    
    <BODY>
    
    
    <form name="myForm">
    <div onClick='getRadios(document.myForm)'>
    
    <b>My Number:</b>
    <input type=radio name="number" value="1">1
    <input type=radio name="number" value="2">2
    <input type=radio name="number" value="3">3
    <input type=radio name="number" value="4">4
    <input type=radio name="number" value="5">5
    
    
    
    
    </div>
    
    
    <b>My Button Values:</b>
    <input type=text name="buttons" size=30>
    
    
    
    
    </form>
    </body>
    Last edited by hawkeyefan; 06-18-2009 at 04:39 PM.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Like this:-

    Code:
    function setRadio() {
    var x = parseInt(document.myForm.buttons.value);
    if (x>= 1 && x <=5) {
    document.myForm.number[x-1].checked = true;
    }
    else {
    document.myForm.buttons.value = "";
    return false;
    }
    }
    
    <b>My Button Values:</b>
    <input type=text name="buttons" size=30 onblur = "setRadio()">
    Why is your textbox size = 30 when the input wil be a single digit?


    "In the beginner's mind there are many possibilities, but in the expert's mind there are few” - Shunryu Suzuki (Japanese Zen priest, ?-1971)

  • Users who have thanked Philip M for this post:

    hawkeyefan (06-18-2009)

  • #3
    New to the CF scene
    Join Date
    Jun 2009
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Works perfect thanks. The 30 should be 3! Thanks for correcting me.
    Last edited by hawkeyefan; 06-18-2009 at 05:54 PM.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Still a single digit! Suggest you add maxlength = 1.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •