[Answer]-Hide/show fields using jquery programmatically through django forms

1šŸ‘

āœ…

Just as some conceptual support:
what you want is to loop through the descendants of a given element and based on a switch, add behavior, right?

You want to try something like:

  • add classnames in appropriate places: ā€˜collapse-stackā€™, ā€˜collapse-labelā€™, ā€˜collapse-listā€™ (label & list might even be just ā€˜collapsableā€™).
  • have jQuery loop through these generic items.

like:

  $(document).ready(function() {
       $('.collapse-stack').each(function(){
            //this = current collapse stack
            $(this).click(function() {
                if ( $(this).is(':checked') ) {
                    $(".collapse-label, .collapse-list").each(function(){
                        //this = current collapse-label
                        $(this).show();
                    });
                }else{
                    $(".collapse-label, .collapse-list").each(function(){
                        //this = current collapse-label
                        $(this).hide();
                    });
                }
            });
        })
    });

or somesuch. Play around with that ā€œeachā€ function.

šŸ‘¤Multifarious

Leave a comment