Jump to content

uk-accordion – problem with multiple accordions


neophron
 Share

Recommended Posts

Hi guys,

I have a page with multiple uk-accordions (https://getuikit.com/docs/accordion). The problem is, that if an user opens an item in one uk-accordion and after that clicks on the next item in a different uk-accordion, the first item stays open. I want to change this in a way, that nevertheless where the user clicks, only one item should be visible.

I found a script and now I'm trying to adopt it:

$('.uk-accordion > li > a').click(function(e) {

      // hide all div.uk-accordion-content
      $('.uk-accordion-content').attr("hidden", true);
      // remove class .uk-open
      $('.uk-accordion > li').removeClass('uk-open');

      $(this).addClass('uk-open');
      // show selected accordion
      var id = $(this).data('.uk-accordion');
      var article = $('article[data-article=' + id + ']');
      article.removeAttr('hidden');
     
    });

    $('.uk-accordion > li > a').click(function(e) {
      
      e.preventDefault();
    });

As you see, my problem is, that I'm not familiar with js.

Link to comment
Share on other sites

Isn't that the default behaviour of the accordions? That's what it would seem like from the examples on the page, the component has a "multiple" option that is set by default on false. Maybe you need to update UIkit?

EDIT: Just re-read, that it's ANOTHER accordion. 

I'd try something like this, just wrote it on the fly so there are probably errors, but the idea is to close any other accordions with the component's API, rather than adding/removing classes. 

var accordions = $('.uk-accordion');

accordions.each(function(index, item){

    UIkit.util.on(item, "show" , function(){

        accordions.not(item).each(function(index, item){

            var opened = $(item).children('.uk-open').index();
            UIkit.accordion(item).toggle(index);

        })

    })

})

 

  • Thanks 1
Link to comment
Share on other sites

5 hours ago, elabx said:

Isn't that the default behaviour of the accordions? That's what it would seem like from the examples on the page, the component has a "multiple" option that is set by default on false. Maybe you need to update UIkit?

EDIT: Just re-read, that it's ANOTHER accordion. 

I'd try something like this, just wrote it on the fly so there are probably errors, but the idea is to close any other accordions with the component's API, rather than adding/removing classes. 


var accordions = $('.uk-accordion');

accordions.each(function(index, item){

    UIkit.util.on(item, "show" , function(){

        accordions.not(item).each(function(index, item){

            var opened = $(item).children('.uk-open').index();
            UIkit.accordion(item).toggle(index);

        })

    })

})

 

Hi, thanks for your help, but right now it doesn't work.
I prepared in codepen an example: https://codepen.io/neophron/pen/mdeaoyK

I also tested the accordion example directly in codepen (https://getuikit.com/docs/accordion) with your script, but with no result.

1140145382_Bildschirmfoto2020-05-20um21_57_23.thumb.jpg.5cf1be6e31cd633abfd2dd2c526fef11.jpg

Link to comment
Share on other sites

Got this working on your codepen: 

 

var accordions = $("[uk-accordion]");
accordions.each(function(index, item){
    UIkit.util.on(item, "beforeshow" , function(e){
         accordions.not(item).each(function(index, item){
           var opened = $(item).children('.uk-open');
           var openItemIndex;
           if(opened.length){
            openItemIndex = opened.index();
           }
           if(openItemIndex !== undefined){
              UIkit.accordion(item).toggle(openItemIndex);
           }
        });
    })
});

 

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...