...

View Full Version : How to set Radio buttons by textbox value



hawkeyefan
06-18-2009, 05:34 PM
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



<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>

Philip M
06-18-2009, 05:58 PM
Like this:-


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)

hawkeyefan
06-18-2009, 06:10 PM
Works perfect thanks. The 30 should be 3! Thanks for correcting me.

Philip M
06-18-2009, 07:34 PM
Still a single digit! Suggest you add maxlength = 1.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum