How to make a radio button when a checkbox button is clicked? And also, how to erase the radio button, when the checkbox is unchecked?
if($("input[type=checkbox]").is(':checked')==true) {
var val = this.value;
var dataSplit = val.split("_");
var result = '<div id="div'+dataSplit[0]+'"><input type="radio" class="choosen"
name="rdb_'+val+'" value="'+val+'" id="'+dataSplit[0]+
'" /><label class="'+dataSplit[0]+'">'+dataSplit[1]+'</label><br /></div>';
$("#result").append(result);
}else if ($("input[type=checkbox]").is(':checked')==false){
var val1 = this.value;
var dataSplit1 = val1.split("_");
$('#div'+dataSplit1[0]).parent.remove();
}
The code works when I check the checkbox, but it doesn’t work when I uncheck the checkbox. When I uncheck, the radio button is still added.
NB: Try to check 2/3 checkboxes, and uncheck them.
You should probably bind it to the change event of the checkbox, and just do:
FIDDLE