Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trigger jquery accordion menu by an event?

Is it possible to open the next panel in a jquery accordion menu through a seperate button onclick event? That is instead of clicking the heading to open another panel, use a button not connected to the accordion.

like image 886
Edward Avatar asked Jun 12 '10 00:06

Edward


People also ask

Which event is triggered when accordion is created?

The jQuery UI Accordion Widget can be used for the conversion of pairs of header & content panels into an accordion. The jQuery UI Accordion create event is used to trigger when the accordion is created.

How do I make my accordion active?

JavaScript Code: accordion({ active: 1 }); var active = $( "#accordion" ). accordion( "option", "active" ); $( "#accordion" ). accordion( "option", "active", 1 ); HTML.

Which event method is not fired for the initial panel when the accordion widget is created?

Note: Since the activate event is only fired on panel activation, it is not fired for the initial panel when the accordion widget is created. If you need a hook for widget creation use the create event.

How do you use an accordion widget?

Drag the accordion widget from the right column onto the page. Choose Edit to enter content. Choose Edit next to an accordion item to add text, images or video. Add an accordion item to create another layer for each additional piece of content you want to list.


2 Answers

Yes, just call activate on the accordion like this:

$("#myaccordion").accordion("activate", 1 );

Where 1 is the index you want to open.

You can get the current zero-based index of the active panel by calling:

var index = $("#myaccordion").accordion('option','active');

So, taking both these items together, we can open the next item on a click:

$("#mybutton").click(function(e){
  e.preventDefault();
  var acc   = $("#myaccordion"),
      index = acc.accordion('option','active'),
      total = acc.children('div').length,
      nxt   = index + 1;

  if (nxt >= total) {
     nxt = 0; // Loop around to the first item
  }

  acc.accordion('activate', nxt);
})
like image 84
Doug Neiner Avatar answered Oct 05 '22 07:10

Doug Neiner


In versions of JQuery UI 1.10 or greater the .activate function has been deprecated in favour of using the 'option' method so an alternative approach using the previous answer and would be:

$("#button").click(function(){
    var index = $("#accordion").accordion('option','active');
    var total = $("#accordion").children('div').length;
    index++;

    // include restart same as previous answer     
    if (index >= total) {
        index = 0;
    }

    $("#accordion").accordion("option", "active", index);

}
like image 36
Crazy Dino Avatar answered Oct 05 '22 08:10

Crazy Dino