jQuery controlled Dependent (or Cascading) Select List 2
tutorial, how-to, Jquery, javascript November 8th, 2007Thank you all. Thanx for using, commenting and visiting my script jQuery controlled Dependent (or Cascading) Select List from my old bolg http://php4bd.wordpress.com and here.
I am getting a lots of request and questions from visitors about adding some features and some other problems. Sometimes I faced some problem when using this script myself. So, I have made some changes here. I hope, this change will help you to overcome some problems of previous version of this script. The main features added in this version are:
- It will keep in child list box only sub items of selected parent value when initializing.
- Added a 4th parameter to input a selected value for child list box.
- When "isSubselectOptional" is true, add a default value ‘none’ for ‘– Select –’ option of child list box.
- Automatically focus the child list box when a value is selected from parent list box.
- More effective for multilevel association.
Click here to see a demo of multilevel association using this function.
Here is the modified function:
1: function makeSublist(parent,child,isSubselectOptional,childVal)
2: {
3: $("body").append("<select style=’display:none’ id=’"+parent+child+"’></select>");
4: $(‘#’+parent+child).html($("#"+child+" option"));
5:
6: var parentValue = $(‘#’+parent).attr(‘value’);
7: $(‘#’+child).html($("#"+parent+child+" .sub_"+parentValue).clone());
8:
9: childVal = (typeof childVal == "undefined")? "" : childVal ;
10: $("#"+child+‘ option[@value="’+ childVal +‘"]’).attr(’selected’,’selected’);
11:
12: $(‘#’+parent).change(
13: function()
14: {
15: var parentValue = $(‘#’+parent).attr(‘value’);
16: $(‘#’+child).html($("#"+parent+child+" .sub_"+parentValue).clone());
17: if(isSubselectOptional)
18: $(‘#’+child).prepend("<option value=’none’> — Select — </option>");
19: $(‘#’+child).trigger("change");
20: $(‘#’+child).focus();
21: }
22: );
23: }
And initialize the association on ‘$(document).ready’, as following example:
1: $(document).ready(function()
2: {
3: makeSublist(’parentID’,‘childID’, true, ‘selected_val_of_child’);
4: });
If you want to create multilevel association, start the initialization from child most order. such as:
1: $(document).ready(function()
2: {
3: makeSublist(‘child’,‘grandson’, true, ”);
4: makeSublist(‘parent’,‘child’, false, ‘3′);
5: });
For 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.











November 13th, 2007 at 9:31 pm
[…] 2nd version of this script is published with some addition and modification. visit the new version here. […]
November 16th, 2007 at 7:47 pm
Excellent work on this! I’m new to jquery, but here is a modified version of your makeSublist() method that has the behavior that I was looking for.. namely disappearing/reappearing sublists, and working on Firefox 2, IE 6, IE 7, and Safari.
function makeSublist(parent, child, isSubselectOptional, childVal) {
$(”body”).append(”");
$(’#’ + parent + child).html($(”#” + child + ” option”));
var parentValue = $(’#’ + parent).attr(’value’);
$(’#’ + child).html($(”#” + parent + child + ” .sub_” + parentValue).clone());
childVal = (typeof childVal == “undefined”) ? “” : childVal;
$(”#” + child + ‘ option[@value=”‘ + childVal + ‘”]’).attr(’selected’,’selected’);
$(’#’ + parent).change(
function() {
var parentValue = $(’#’ + parent).attr(’value’);
$(’#’ + child).html($(”#” + parent + child + ” .sub_” + parentValue).clone());
if (isSubselectOptional) {
$(’#’ + child).prepend(” — More — “);
}
if (document.getElementById(child)) {
if (document.getElementById(child).options.length == 1) {
document.getElementById(child).style.visibility = ‘hidden’;
} else {
document.getElementById(child).style.visibility = ‘visible’;
$(’#’ + child).trigger(”click”);
}
} else {
$(’#’ + child).trigger(”click”);
}
$(’#’ + child).trigger(”change”);
}
);
}
–
Jason
Co-author, Tomcat: The Definitive Guide, 2nd Edition
November 21st, 2007 at 8:02 am
[…] Combobox. jQuery controlled dependent (or Cascadign) Select List. Multiple Selects. Select box manipulation. Select Combo Plugin. jQuery - LinkedSelect […]
November 22nd, 2007 at 3:46 pm
Great… I’m a newbie at JS… i need to redirect the visitor to a specific url once the user choosed the an option from the second dropdown:
I mean;
1) User selects one option from first dropdown.
2) User selects one option from second dropdown.
3) User is redirected to a specific #
Thanks!
November 29th, 2007 at 1:55 am
@Martin
Hi Martin, Thanx for ur question.
I think, u may already get the solution. If not, please follow this steps:
1. Set the urls, where u want to rediract as the values of child select box.
2. On change event of child select box, call a function to rediract the page like this:
$(”child_select_id”).change( function() {
var theURL = $(”child_select_id”).val();
window.location = theURL ;
} );
Define this binding in the ready enent of document.
Please let me know if any more confution here.
December 1st, 2007 at 6:29 pm
I am new to developing web sites with some very basic knowledge of PHP, some MySQL and a bit of HTML and almost none of Java. I have been trying to have 3 dependat option boxes. I tried to incorporate your script into the data extraction from MySQL to narrow down the department name down to financial years and further down to months for available reports to view. I am getting very ad-hoc results. I have listed my script here and I would greatly appreciate any help. Many thanks:
function makeSublist(parent,child,isSubselectOptional,childVal)
{
$(”body”).append(”");
$(’#'+parent+child).html($(”#”+child+” option”));
var parentValue = $(’#'+parent).attr(’value’);
$(’#'+child).html($(”#”+parent+child+” .sub_”+parentValue).clone());
childVal = (typeof childVal == “undefined”)? “” : childVal ;
$(”#”+child+’ option[@value=”‘+ childVal +’”]’).attr(’selected’,’selected’);
$(’#'+parent).change(
function()
{
var parentValue = $(’#'+parent).attr(’value’);
$(’#'+child).html($(”#”+parent+child+” .sub_”+parentValue).clone());
if(isSubselectOptional) $(’#'+child).prepend(” — Select — “);
$(’#'+child).trigger(”change”);
$(’#'+child).focus();
}
);
}
$(document).ready(function()
{
makeSublist(’child’,'grandson’, true, ”);
makeSublist(’parent’,'child’, false, ‘1′);
});
– Select Dept –
“. $row[’dept_name’] .”";
}
?>
“.$row[’fy’].”";
}
?>
“. $row[’month’] .”";
}
?>
December 1st, 2007 at 6:32 pm
Back again, I was just reviewing my logged message. It appears that all my PHP bits are missed!!
December 5th, 2007 at 5:35 am
[…] Combobox jQuery controlled dependent (or Cascadign) Select List Multiple Selects Select box manipulation Select Combo […]
December 5th, 2007 at 6:17 pm
[…] Combobox. jQuery controlled dependent (or Cascadign) Select List. Multiple Selects. Select box manipulation. Select Combo Plugin. jQuery - LinkedSelect […]
December 14th, 2007 at 3:57 pm
This is really great..but just can’t seem to get 6 levels working in IE or Safari. Also, I’m very interested in an example of how to make certain items open links.
Is there anyway you could slap together a quick 6 level example with a few links? this would be so perfect!
thanks!
January 2nd, 2008 at 12:56 am
[…] Username Check with jQuery.三、表单-选取框(Form - Select Box stuff)jQuery Combobox.jQuery controlled dependent (or Cascadign) Select List.Multiple Selects.Select box manipulation.Select Combo Plugin.jQuery - LinkedSelectAuto-populate […]
January 22nd, 2008 at 9:11 am
Hi,
Please note that in the latest version of Jquery 1.2.2, this does not work in IE browser (6.XX)
The older version of Clone function is quite different from the new version of Jquery.
TO workaround, you acn copy the clone fuction from old Jquery to the nwe jquery script and name it as clone_old and use clone_old in the line and it should look like this:
$(‘#’+child).html($(”#”+parent+child+” .sub_”+parentValue).clone_old());
Is there any other workaround ?
Anand (acpadhi@twindia.com) &
Shankar (shanki1969@yahoo.co.in)
January 24th, 2008 at 7:27 pm
[…] Combobox. jQuery controlled dependent (or Cascadign) Select List. Multiple Selects. Select box manipulation. Select Combo Plugin. jQuery - LinkedSelect […]
February 12th, 2008 at 10:34 pm
[…] Combobox. jQuery controlled dependent (or Cascadign) Select List. Multiple Selects. Select box manipulation. Select Combo Plugin. jQuery - LinkedSelect […]
February 17th, 2008 at 8:27 am
[…] jQuery controlled dependent (or Cascadign) Select List […]
March 3rd, 2008 at 4:07 am
[…] Combobox. jQuery controlled dependent (or Cascadign) Select List. Multiple Selects. Select box manipulation. Select Combo Plugin. jQuery - LinkedSelect […]
March 4th, 2008 at 1:41 pm
Great work.
The script is very good, here it is an idea for a new feature. If you use tag inside , the script will just ignore it. It would be nice to display also the label.
March 4th, 2008 at 1:42 pm
Great work.
The script is very good, here it is an idea for a new feature. If you use optgroup tag inside select, the script will just ignore it. It would be nice to display also the optgroup label.
March 11th, 2008 at 5:22 pm
it seems like e very good web site but my Chinese is not good. It would be great if it might be availible in English too. Thanks.
March 19th, 2008 at 6:15 pm
[…] Combobox jQuery controlled dependent (or Cascadign) Select List Multiple Selects Select box manipulation Select Combo Plugin jQuery - LinkedSelect Auto-populate […]
March 30th, 2008 at 9:20 am
[…] jQuery controlled dependent (or Cascadign) Select List […]
March 31st, 2008 at 5:19 am
[…] jQuery controlled dependent (or Cascadign) Select List […]
April 9th, 2008 at 2:57 am
[…] Combobox. jQuery controlled dependent (or Cascadign) Select List. Multiple Selects. Select box manipulation. Select Combo Plugin. jQuery - LinkedSelect […]
April 22nd, 2008 at 6:17 am
Warning: Error in parsing value for property ‘opacity’. Declaration dropped.
April 26th, 2008 at 4:16 pm
I am wanting to incorporate your script into a dynamic php+mySQL app, and it works WONDERFULLY for creating new records. Problem is encountered when I attempt to show user an existing record for editing that has an existing value for child field. How do you get the initialization function to recognize and retain the value passed from the database.
April 26th, 2008 at 4:23 pm
Forget it, I figured it out. I added a line to the initialization function
childValue = $(’#'+child).attr(’value’);
and then used that variable in makeSublist().
This is script is just what I was looking for, very clean and efficient! Many thanks for sharing!
April 26th, 2008 at 4:53 pm
Sorry to bother you again. But making that last change has introduced other problems. Now the child does not filter values based on parent change it shows all values available and does not change. *darn it*!
April 27th, 2008 at 10:45 am
Got it! (with a small *duh*) I was quite obviously calling for the value of the child element incorrectly for use in the makeSublist function. Thanks again for sharing solution and providing supporting explanation.
May 2nd, 2008 at 9:32 pm
[…] Combobox. jQuery controlled dependent (or Cascadign) Select List. Multiple Selects. Select box manipulation. Select Combo Plugin. jQuery - LinkedSelect […]
May 8th, 2008 at 10:01 pm
[…] Combobox. jQuery controlled dependent (or Cascadign) Select List. Multiple Selects. Select box manipulation. Select Combo Plugin. jQuery - LinkedSelect […]