JSFiddle - React, Tailwind, and code Playground
by Zubair Mushtaq
HTML
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/css/select2.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js"></script>
<select name="school_id[]" id="school_id" class="regular-text select2-hidden-accessible" multiple="" tabindex="-1" aria-hidden="true">
<option value="7" selected="selected">MCS</option>
<option value="8">JPPS</option>
</select>
<select name="campus_id[]" id="campus_id" class="regular-text select2-hidden-accessible" multiple="" style="width:100%;" tabindex="-1" aria-hidden="true">
<option value="16"> Johar Campus </option>
<option value="11"> MCH Boys Branch </option>
<option value="13"> MCH Girls Branch </option>
<option value="14"> Boys Campus </option>
<option value="15"> JPPS Shuja Abad Campus </option>
</select>
CSS
/* Space out content a bit */
body {
padding-top: 20px;
padding-bottom: 20px;
}
/* Everything but the jumbotron gets side spacing for mobile first views */
.header,
.marketing,
.footer {
padding-right: 15px;
padding-left: 15px;
}
/* Custom page header */
.header {
padding-bottom: 20px;
border-bottom: 1px solid #e5e5e5;
}
/* Make the masthead heading the same height as the navigation */
.header h3 {
margin-top: 0;
margin-bottom: 0;
line-height: 40px;
}
/* Custom page footer */
.footer {
padding-top: 19px;
color: #777;
border-top: 1px solid #e5e5e5;
}
/* Customize container */
@media (min-width: 768px) {
.container {
max-width: 730px;
}
}
.container-narrow > hr {
margin: 30px 0;
}
/* Supporting marketing content */
.marketing {
margin: 40px 0;
}
.marketing p + h4 {
margin-top: 28px;
}
/* Responsive: Portrait tablets and up */
@media screen and (min-width: 768px) {
/* Remove the padding we set earlier */
.header,
.marketing,
.footer {
padding-right: 0;
padding-left: 0;
}
/* Space out the masthead */
.header {
margin-bottom: 30px;
}
/* Remove the bottom border on the jumbotron for visual effect */
.jumbotron {
border-bottom: 0;
}
}
JavaScript
jQuery(function($) {
// Select2 dropdown for school
$('#school_id').select2({
placeholder: 'Select a school',
width: 300
});
// Select2 dropdown for campuses
$('#campus_id').select2({
placeholder: 'Select a Campus',
width: 300
});
var campuses = {"7":{"16":"Johar Campus","11":"MCH Boys Branch","13":"MCH Girls Branch"},"8":{"14":"Boys Campus","15":"JPPS Shuja Abad Campus"}};
$('#school_id').on('change',function() {
var id = $(this).val();
var i = campuses[id];
console.log(i);
$('#campus_id option').remove();
$('#campus_id')
.append($("<option></option>")
.attr("value",'')
.text('Select Semester'));
// Add option to the select box from list
if(i !== null) {
$.each(i, function(key, value) {
$('#campus_id')
.append($("<option></option>")
.attr("value",key)
.text(value));
});
}
});
});