nav-left cat-right
cat-right

jQuery controlled Dependent (or Cascading) Select List 2

Thank 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:

  1. It will keep in child list box only sub items of selected parent value when initializing.
  2. Added a 4th parameter to input a selected value for child list box.
  3. When "isSubselectOptional" is true, add a default value ‘none’ for ‘– Select –’ option of child list box.
  4. Automatically focus the child list box when a value is selected from parent list box.
  5. 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.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Netvouz
  • description
  • Wists
  • Furl
  • Ma.gnolia
  • NewsVine
  • Reddit
  • Simpy
  • StumbleUpon
  • Technorati


123 Responses to “jQuery controlled Dependent (or Cascading) Select List 2”

  1. [...] jQuery controlled dependent (or Cascadign) Select List. [...]

  2. [...] jQuery controlled dependent (or Cascadign) Select List. [...]

  3. TuxX GERMANY says:

    Hey Pablo,

    use this:

    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).val(["'+ 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’,'grandsun’, true, ”);
    makeSublist(’parent’,'child’, false, ‘1′);
    });

  4. TuxX GERMANY says:

    JQuery >3 has a new selectors engine:

    >>OLD: <>NEW: <<
    $(”#”+child).val(["'+ childVal +'"]).attr(’selected’,’selected’);

  5. [...] jQuery controlled dependent (or Cascadign) Select List [...]

  6. [...] jQuery controlled dependent (or Cascadign) Select List [...]

  7. [...] Combobox jQuery controlled dependent (or Cascadign) Select List Multiple Selects Select box manipulation Select Combo Plugin jQuery - LinkedSelect Auto-populate [...]

  8. African Boy says:

    It would have made sense if you had placed a downloadable sample

  9. Anis Ahmad HONG KONG says:

    @African Boy,

    Thanks a lot suggestion.
    I’ll do it soon.

  10. Thanks for the code snippet.

    With jquery 1.2.6 it works fine.

    Under jquery 1.3.2 it doesnt. The console has the error:
    [Exception... "'Syntax error, unrecognized expression: [@value="1"]‘ when calling method: [nsIDOMEventListener::handleEvent]” nsresult: “0×8057001e (NS_ERROR_XPC_JS_THREW_STRING)” location: “” data: no]

    Any ideas?
    Thanks,
    Andy

  11. varyusya DENMARK says:

    Saving an 11-run lead

  12. vasyana DENMARK says:

    Thank you author

  13. Frederick says:

    A downloadable sample or source code for a simple form with 2 or three select boxes would go a long way in furthering my understanding of how to implement this.

  14. [...] jQuery controlled dependent (or Cascadign) Select List. [...]

  15. lyubava DENMARK says:

    Colorflow 1.0

  16. jason UNITED KINGDOM says:

    hi

    This is great and exactly what i’m looking for - however i cant get the script working with jquery 1.3.2. do you have any ideas please?

    Thanks

  17. [...] Combobox. jQuery controlled dependent (or Cascadign) Select List. Multiple Selects. Select box [...]

  18. [...] Combobox. jQuery controlled dependent (or Cascadign) Select List. Multiple Selects. Select box manipulation. Select Combo Plugin. jQuery – LinkedSelect [...]

  19. carlo UNITED STATES says:

    Does anyone know how to make this work with jQuery 1.3.2?

  20. Xarakiry RUSSIAN FEDERATION says:

    This is really great plugin for JQuery! I like it )))))

Leave a Reply