I have 4 radio buttons. When I press one, the others change. But how can I deselect the radio button I selected earlier? I though of this but it doesn't work:
var id = $(this).attr("id");
id.setSelected(false);
I have 4 radio buttons. When I press one, the others change. But how can I deselect the radio button I selected earlier? I though of this but it doesn't work:
var id = $(this).attr("id");
id.setSelected(false);
Give all radio inputs the same name. See the example below. For this, you don't need JQuery, it's all managed by the browsers.
HTML
<form>
<input type="radio" name="myRadio">
<input type="radio" name="myRadio">
<input type="radio" name="myRadio">
<input type="radio" name="myRadio">
<input type="submit">
</form>
<body>
<input type='radio' class='radio-button' name='rb'>
<input type='radio' class='radio-button' name='rb'>
<input type='radio' class='radio-button' name='rb'>
</body>
<script type="text/javascript">
var allRadios = document.getElementsByName('rb');
var thisRadio;
var x = 0;
for(x = 0; x < allRadios.length; x++){
allRadios[x].onclick = function() {
if(thisRadio == this){
this.checked = false;
thisRadio = null;
}else{
thisRadio = this;
}
};
}
</script>
To deselect all radio button you have to add Reset button