How to populate html dropdown based on another dropdown using Jquery | Sanwebcorner

How to populate html dropdown based on another dropdown using Jquery

Populate the Dropdown based on another dropdown is very easy and simple, Now we will see how to do this without any database using jquery and javascript. This will done by using html dropdown or select box, It is dynamically display the particular dropdown data based on main html dropdown. In this example i am using the list of branch in main dropdown, the branches like Machanical , css&IT, Civil, EEE based on this select box display the differnt courses when you select the machanical the machanical course will display the another html dropdown, this will automatically populated based on main dropdown.

 This example completely working with the help of Jquery and javascript. The below javascript code is used to poplulate the data to the dropdown.

<script type="text/javascript">
var courseByCategory = {
Mechanical: ["Mec-course-1", "Mec-course-2", "Mec-course-3", "Mec-course-4"],
Civil: ["Civ-course-1", "Civ-course-2", "Civ-course-3", "Civ-course-4"],
IT: ["IT-course-1", "IT-course-2", "IT-course-3", "IT-course-4"],
EEE: ["EEE-course-1", "EEE-course-2", "EEE-course-3", "EEE-course-4"]
}
function changecat(value) {
if (value.length == 0) document.getElementById("course").innerHTML = "<option></option>";
else {
var catOptions = "";
for (categoryId in courseByCategory[value]) {
catOptions += "<option>" + courseByCategory[value][categoryId] + "</option>";
}
document.getElementById("course").innerHTML = catOptions;
}
}
</script>

The below example haves the demo link and download link you will visit the demo page how the html dorpdown populate based on the another dropdown or select box and i also provided the download link you can download and use the code based on your requirement. It may very useful in various places in your web application development. I hope this is very useful to you.




Code for html dorpdown based on another dropdown:


<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>How to populate html dropdown based on another dropdown using Jqery</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
<!-- TODO: Missing CoffeeScript 2 -->
<script type="text/javascript">
var courseByCategory = {
Mechanical: ["Mec-course-1", "Mec-course-2", "Mec-course-3", "Mec-course-4"],
Civil: ["Civ-course-1", "Civ-course-2", "Civ-course-3", "Civ-course-4"],
IT: ["IT-course-1", "IT-course-2", "IT-course-3", "IT-course-4"],
EEE: ["EEE-course-1", "EEE-course-2", "EEE-course-3", "EEE-course-4"]
}

function changecat(value) {
if (value.length == 0) document.getElementById("course").innerHTML = "<option></option>";
else {
var catOptions = "";
for (categoryId in courseByCategory[value]) {
catOptions += "<option>" + courseByCategory[value][categoryId] + "</option>";
}
document.getElementById("course").innerHTML = catOptions;
}
}
</script>


<style>
select
{
padding: 12px;
min-width:280px;
margin-top:10px;
}

.san{
width:280px;
margin:0 auto;
margin-top:90px;
background-color:#009688;
padding:55px;
}

label
{
color:#fff;
margin-bottom:25px;
}

html{
    background-color: #0aa798;
}
</style>

</head>
<body>
<div class="san">
<label>Select Branch: &nbsp; &nbsp; &nbsp; </label>
<select name="branch" id="branch" onChange="changecat(this.value);">
<option value="" disabled selected>Select</option>
<option value="Mechanical">Mechanical</option>
<option value="Civil">Civil </option>
<option value="IT">Csc & IT</option>
<option value="EEE">EEE</option>
</select>
<br/><br/>

<label>Select Course:  &nbsp; &nbsp; &nbsp; </label>
<select name="course" id="course">
<option value="" disabled selected>Select</option>
</select>
</div>
<h1 style="text-align:center; text-decoration:none; color:#fff;"><a style="text-decoration:none; color:#fff;" href="http://www.sanwebcorner.com/">www.sanwebcorner.com</a></h1>
</body>
</html>

Post a Comment

Labels

360 degree view product image 3d rotate 3d text 3step-form Accordion menu ajax All Browsers Animated Background Gradient animation audio controller auto complete Best accordion menu bootstrap border bounce effect on hover Breadcrumb calculate age by dob calendar Captcha change image on mouse move color chooser countdown Counter on scroll Css css animation css buttons css hover css3 customized upload Button Date picker delete data from table Different scroll speed email otp verification email verifier Embed eventlist Expanding Menu feedbck form flash news section close button full screen menu Google custom search Google Maps Google reCAPTCHA Google site search gradient hide menu Horizontal News ticker Hover Htm Html html dropdown html form html5 html5 form validation Iframe image hover increment decrement textbox value based on select box Javascript Jquery js Layered image effect on mouse hover lock & unlock page scroll marquee marquee with close button mobile number validation mobile otp login Multi-level-drop-down-menu multi-level-form Multi-Step-form mysql News ticker on hover change text to image our team page password strength checker Photoshop PHP populate html selectbox Popup image on click the link pure css Question type form Random password Redirect page Based on the select box while submit the form responsive responsive menu responsive popup box Responsive Vertical Time line design rotate image selectbox based on select box simple coming soon page simple countdown based on end date simple typing effect slider sliding tab sms otp login statistic counter on scroll switch tag shape team member Time picker toggle toggle view transparent button underline animation up down slide validation vertical fixed sidebar Webtips Youtube