In: Computer Science
This is in HTML and JavaScript
So I have 2 radio buttons One called students and one called teacher. If the user selects the student then a list of check boxes these should appear. These should not be visable if the user selects the teacher radio button.
- buy text books
- visit teachers website
- attend class
If the user selects the teacher radio button then all the text boxes above should be hidden From the user. Then these check boxes should be appear.
- payment
- class time
- class list.
But if the user selects the student radio button then the check boxes above should not be visable.
HTML code:
Code is commented for better understanding
<!DOCTYPE html>
<html>
<head>
<!-- import jquery.min.js library -->
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<!-- css for styling-->
<style>
label{
display: block;
padding: 4px;
}
#primary{
margin-bottom: 10px;
}
</style>
</head>
<!-- write the body here -->
<body>
<!-- create a form -->
<form>
<fieldset id="primary">
<legend>radio buttons</legend>
<label><input type="radio" name="primary"
data-group="#subOptions1"> student</label>
<label><input type="radio" name="primary"
data-group="#subOptions2"> teacher</label>
</fieldset>
<!-- create radio buttons for both teacher and student-->
<fieldset id="secondary">
<legend>check boxes</legend>
<div id="subOptions1">
<label><input type="checkbox"> buy text
books</label>
<label><input type="checkbox"> visit teachers
website</label>
<label><input type="checkbox"> attend
class</label>
</div>
<div id="subOptions2">
<label><input type="checkbox">
payment</label>
<label><input type="checkbox"> class
time</label>
<label><input type="checkbox"> class
list</label>
</div>
</fieldset>
</form>
<script>
//write a function to disable checkboxes
function disable(selector){
$(selector).hide();
$(selector).find(":checkbox").each(function(){
$(this).prop("disabled", true)
});
}
//write a function to enable checkboxes
function enable(selector){
$(selector).show();
$(selector).find(":checkbox").each(function(){
$(this).prop("disabled", false)
});
if($("#secondary").is(":hidden")){
$("#secondary").show();
}
}
<!-- function to handle radio buttons change -->
function handleRadioButtonChange(){
var numChecked = $("#primary").find(":checked").length; // check if
primary is selected
if(numChecked === 0){ // then secondary is enabled
disable("#secondary");
} else { // else primary is diabled/ hid
var showGroup = $(this).data("group"),
hideGroup =
$("#primary").find(":radio:not(:checked)").data("group"); //
display checkboxes
disable(hideGroup);
enable(showGroup);
}
}
var $radioButtons = $("#primary :radio"); // assign the radio
button values
$radioButtons.on("change", handleRadioButtonChange); // call the
function here
$radioButtons.trigger("change"); // trigger the radio buttons
</script>
</body>
</html>
output:
when student is selected, only 3 checkboxes as said in question will be displayed
when teacher is selected, only 3 checkboxes for teachers as said in question will be displayed