I have the radio button with the name "Choose" with the options yeas and no. If i selects any one ofthe option and clicking the button "clear", i need javascript to clear the selected option. So how to proceed with that.
Thanks
I have the radio button with the name "Choose" with the options yeas and no. If i selects any one ofthe option and clicking the button "clear", i need javascript to clear the selected option. So how to proceed with that.
Thanks
This should work. Make sure each button has a unique ID. (Replace Choose_Yes and Choose_No with the IDs of your two radio buttons)
document.getElementById("Choose_Yes").checked = false;
document.getElementById("Choose_No").checked = false;
An example of how the radio buttons should be named:
<input type="radio" name="Choose" id="Choose_Yes" value="1" /> Yes
<input type="radio" name="Choose" id="Choose_No" value="2" /> No
YES<input type="radio" name="group1" id="sal" value="YES" >
NO<input type="radio" name="group1" id="sal1" value="NO" >
<input type="button" onclick="document.getElementById('sal').checked=false;document.getElementById('sal1').checked=false">
Wouldn't a better alternative be to just add a third button ("neither") that will give the same result as none selected?
You don't need to have unique id for the elements, you can access them by their name attribute:
If you're using name="Choose"
, then:
With jQuery it is as simple as:
$('input[name=Choose]').attr('checked',false);
or in pure Javascript:
var ele = document.getElementsByName("Choose"); for(var i=0;i<ele.length;i++) ele[i].checked = false;
Simple, no jQuery required:
<a href="javascript:clearChecks('group1')">clear</a>
<script type="text/javascript">
function clearChecks(radioName) {
var radio = document.form1[radioName]
for(x=0;x<radio.length;x++) {
document.form1[radioName][x].checked = false
}
}
</script>