Forum Moderators: open
<form action="http ://www.domain.com/file.php?value=[i want the radio button value here]" method="post" id="SearchForm">
<input type="text" value="" name="search" />
<select name="options">
<option selected="selected">a</option>
<option>b</option>
</select>
<label for="number0"><input value="1" name="number" id="number0" type="radio"> 1</label>
<label for="number1"><input value="2" name="number" id="number1" type="radio"> 2</label>
<a href="#" onclick="document.getElementById('SearchForm').submit()">GO</a>
<input onclick="alert('Checked value is: '+getCheckedValue(document.forms['SearchForm'].elements['number']));" value="Show Checked Value" type="button">
// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
function getCheckedValue(radioObj) {
if(!radioObj)
return "";
var radioLength = radioObj.length;
if(radioLength == undefined)
if(radioObj.checked)
return radioObj.value;
else
return "";
for(var i = 0; i < radioLength; i++) {
if(radioObj[i].checked) {
return radioObj[i].value;
}
}
return "";
}
// set the radio button with the given value as being checked
// do nothing if there are no radio buttons
// if the given value does not exist, all the radio buttons
// are reset to unchecked
function setCheckedValue(radioObj, newValue) {
if(!radioObj)
return;
var radioLength = radioObj.length;
if(radioLength == undefined) {
radioObj.checked = (radioObj.value == newValue.toString());
return;
}
for(var i = 0; i < radioLength; i++) {
radioObj[i].checked = false;
if(radioObj[i].value == newValue.toString()) {
radioObj[i].checked = true;
}
}
}
<form id=SearchForm action=http://www.example.com/file.php?value=1>
<input value="1" name="number" id="number0" type="radio" checked> 1
<input value="2" name="number" id="number1" type="radio"> 2
<input onclick="setAction();" value="Submit" type="submit">
</form>
<script type=text/javascript>
function setAction() {
theForm = document.getElementById('SearchForm');
radioGroup = theForm.elements['number'];
for (var i=0; i < radioGroup.length; i++) {
if (radioGroup[i].checked) theForm.action = "http://www.example.com/file.php?value=" + radioGroup[i].value;
}
}
</script>
theForm.submit();