jQuery controlled Dependent (or Cascading) Select List
Jquery, javascript July 15th, 2007When I was searching the web for a client side dependent list boxes, I got some scripts. But some of them were very much static (static select name, option name etc.), some were vary complex. Then I thought to make it myself.
I have been using jQuery as the standard JavaScript library for most of my web project for some days. Writing javascript coding in jQuery is fun. So here goes my contribution using it.
Here is a simple demo.
Let me guide you through it.
1. Include jqury.js.
<script language=”javascript” src=”jquery.js”></script>
You can get Jquery from here.
2. Write this simple function in a javascript block in head.
function makeSublist(parent,child,isSubselectOptional)
{
$(”body”).append(”<select style=’display:none’ id=’”+parent+child+”‘></select>”);
$(’#'+parent+child).html($(”#”+child+” option”));
$(’#'+child).html(”<option> — </option>”);
$(’#'+parent).change(
function()
{
var parentValue = $(’#'+parent).attr(’value’);
$(’#'+child).html($(”#”+parent+child+” .sub_”+parentValue).clone());
if(isSubselectOptional) $(’#'+child).prepend(”<option> — Select — </option>”);
}
);
}
This function takes 3 arguments: the parent select input’s id, the child select input’s id, and a boolean value to indicate whether to select an item from child list by default.
Example: makeSublist(’listA’,'listB’,false);
This function will make the options of ‘listB’ list depending on the selection of ‘listA’. And user have to select an item from ‘listB’.
Here ‘listA’ and ‘listB’ are the IDs of parent and child select input respectively.
3. Add a class to the ‘<option>’s of the child list box. The class name will be the value of it’s parent ‘<option>’ in parent listbox with a ’sub_’ prefix.
Suppose, in parent listbox there is an item “Flower”. Its value is ‘fl’:
<option value=’fl’>Flower</option>
When the item “Flower” will be selected, only 3 items should be visible in child listbox. These 3 items should hold the class name ’sub_fl’:
<option class=”sub_fl” value=”1″>Rose</option>
<option class=”sub_fl” value=”2″>Sunflower</option>
<option class=”sub_fl” value=”3″>Orchid</option>
4. Now you are ready. On ‘$(document).ready’ event of Jquery, run the function to associate your list boxes.
$(document).ready(function()
{
makeSublist(’parentID’,'childID’, false);
});
5. Enjoy ![]()











July 16th, 2007 at 5:59 am
Start of a new epic, finally!!
Welcome to the wonderful world of bloggers.
July 16th, 2007 at 6:00 am
Nice. It’s a great work…I hope u will do more for helping us…
keep it up..
July 16th, 2007 at 2:09 pm
nice function but where is the dynamic part ?? i am always in search of a such script but getting the values from the db, so it need some php works , don’t you think ??
July 16th, 2007 at 4:46 pm
Well done! Keep it up.
Murad.uk
July 16th, 2007 at 6:57 pm
Nice Blog.
July 16th, 2007 at 9:18 pm
Hi Mohamed Badr,
Thanx for ur comment.
Here the dynamic matter is that, you can set option values from database using php and can make multiple and multilevel dependent listbox.
You can use this simple technique to set list options from db using php:
<select name=”parentList” id=”parentList” size=”1″>
<option value=”none”>– Select one –</option>
<?php
while($row = mysql_fetch_array($result, MYSQL_BOTH))
{
echo “<option value=” . $row[’optionValue’] . “>”. $row[’optionText’] .”</option>”;
}
?>
</select>
And for child lists :
<select name=”childList” id=”childList” size=”1″>
<?php
//subItems of Man
while($row = mysql_fetch_array($resultMan, MYSQL_BOTH))
{
echo “<option class=’sub_men’ value=” . $row[’optionValue’] . “>”. $row[’optionText’] .”</option>”;
}
//subItems of Woman
while($row = mysql_fetch_array($resultWomen, MYSQL_BOTH))
{
echo “<option class=’sub_women’ value=” . $row[’optionValue’] . “>”. $row[’optionText’] .”</option>”;
}
?>
</select>
Now your class names of child list is organised and you can use the function to make them associate.
Thanx again.
July 18th, 2007 at 10:35 am
Perhaps a more semantic way to do this would be with optgroups? You shouldn’t need to do much with IDs then. This would convert 1 select box with multiple sub-options into 2 select boxes as you have done.
Have a look at http://www.wait-till-i.com/index.php?p=411
Should be easy to convert that to JQuery.
July 18th, 2007 at 1:27 pm
Hi Dave,
Thanx a lot. It’s really a nice way.
But, the problem in this technique is, a parent option without any child option is not possible here. But sometimes, we need the flexibility to use options which may have or may have not any child option.
However, I m thinking to write another function based on this technique and automating next processes using jQuery.
Thanx again for ur suggestion.
August 20th, 2007 at 10:09 am
How would you add a third or forth select option.
August 20th, 2007 at 5:45 pm
Hi webdube,
Its very easy
For multiple dependent select lists :
$(document).ready(function()
{
makeSublist(’parentID1’,’childID1’, false);
makeSublist(’parentID2’,’childID2’, false);
makeSublist(’parentID3’,’childID3’, false);
});
For multilevel dependent select lists :
$(document).ready(function()
{
makeSublist(’selectID1’,’selectID2’, false);
makeSublist(’selectID2’,’selectID3’, false);
makeSublist(’selectID3’,’selectID4’, false);
});
Please tell me if any confusion.
Thanks
September 30th, 2007 at 11:13 am
superb logo! i really love it
www.funbangla.com
October 25th, 2007 at 8:18 pm
I have visited your site 713-times
November 8th, 2007 at 11:40 pm
[…] details instruction and example of using this script, please visit the previous version. Feel free to ask me if any problem with using this script or any bug you found. Share and Enjoy: […]
February 5th, 2008 at 3:55 pm
great but, why don’t implement it as a jQuery plugin?