kk_ kk_ - 1 year ago 162
Javascript Question

Semantic Ui Accordion get number of elements

I'm using a Semantic Ui accordion and want the user to be able to print the accordion with all elements. (It's like a FAQ accordion)

Is there a way to open all accordion elements or get the index of the last element?

Here is my javascript code:

$('.print-btn').on('click', function(){
$('.ui.accordion').accordion('open', 0);;
setTimeout(function() { //wait a second until the accordion is opened
}, 1000);

This would open the first element and then print it. So I practically need a loop to open all elements and for that I need the number of elements.

I don't know the number of elements beforehand, because the data can change through the database.

Answer Source

You can mark all your FAQ items with a specific class and get all items with the specified class selector (I use jQuery here):

var items = $('.faq-item.ui.accordion');

After that it's trivial to go through each item from the array items and open them with a for-loop:

for (int i=0; i<items.length; i++) {
  items.accordion('open', i)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download