Advertisement
dimkiriakos

poutsa accordition js

Jul 22nd, 2022
748
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     (function ($) {
  2.       // Add class to children only.
  3.       $('.js-form-item:has(span.field-prefix)').addClass('is-child');    
  4.       // Use a counter to be able to move children to the right container.
  5.       let counter = 0;
  6.       $(".js-form-item").each(function(){
  7.         if(!$(this).hasClass('is-child')) { //Parent: Add class and container
  8.           counter++;
  9.           $(this).addClass('parent-' + counter + ' js-parent');
  10.           $(this).append('<div class="container-' + counter + ' js-container"></div>')
  11.         }
  12.         else { // Child: move them to the parent's container.
  13.           $('.container-' + counter).append($(this));
  14.         }
  15.       });
  16.       // Hide all children.
  17.       $('.js-container').slideUp();
  18.       // On parent click, hide all other containers and show the current one.
  19.       $('.js-parent').click(function(e){
  20.         var isChild =  $(e.target).closest('div').hasClass('is-child');
  21.         //console.log(current.hasClass('is-child'));
  22.        
  23.                 if (!isChild){
  24.            $('.js-container').slideUp();
  25.           $(this).find('.js-container').slideDown();      
  26.      
  27.      
  28.         }      
  29.      
  30.        
  31.      
  32.         return false;
  33.       });
  34.      
  35.     })(jQuery);
  36.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement