Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery accordion: prevent pane from opening/cancel changestart event

I've got the following markup:

<div id="accordion" class="leftaligned">
    <div>
        <h3><a href="#">Stakeholder</a></h3>
        <div>Content</div>
    </div>
    <div>
        <h3><a href="#">Relationships</a></h3>
        <div>Blah blah</div>
    </div>
    <div>
        <h3><a href="#">Address</a></h3>
        <div>Yada yada</div>
    </div>
    <div>
        <h3><a href="#">Contact Details</a></h3>
        <div>Foo bar</div>
    </div>
</div>

I create an accordion as follows:

$("#accordion").accordion({
    header: "h3",
    fillSpace: true,
    changestart: function(event, ui) {
        if (someConditionIsTrue()) {
            event.stopPropagation();
            event.preventDefault();
            return (false);
        }
    }
});

The idea is that there are some use cases which would prevent the user from changing panes, however the above cancelling of the event has no effect and the panes can still be changed.

Is there a way to prevent the changing of panes? I also tried activating the current pane programmatically in order to prevent the change, but that fires another changestart event and all hell breaks loose (the accordion actually breaks)

like image 476
Veli Gebrev Avatar asked Jan 05 '10 08:01

Veli Gebrev


People also ask

How do you close an accordion when another jQuery opens?

Assuming you are using the jQuery UI accordion, you can collapse all sections with . accordion('activate', false) . First, in your HTML, give all accordion containers class="accordion" to make them more readily addressable as a group. You can keep the id="accordion1" etc.

How to stop accordion in jQuery?

You can try the following if you are positive about the position the tab to be disable has: // Disable the first tab $( "#accordion > h3:first-child" ). addClass( "ui-state-disabled" ); // Make sure the fourth tab is enabled $( $( "#accordion > h3" )[3] ). removeClass( "ui-state-disabled" );

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.


2 Answers

Maybe it's available in older jQueryUIs but if you use jQuery-UI 1.9.2 or newer you can disable accordion collapsing in beforeActivate event;

beforeActivate: function (event, ui) {
    event.preventDefault();
}
like image 150
Borbea Avatar answered Sep 27 '22 03:09

Borbea


Before initing accordion add your custom click handler with your logic. stopImmediatePropagation will stop event before accordion handler will be called.

$('.quiz-qa h3').click(function(e) {      
  if ($(this).hasClass("deleted")) {
    e.stopImmediatePropagation();
    return false;      
   }   
});    
$('.quiz-qa').accordion();
like image 28
user438316 Avatar answered Sep 26 '22 03:09

user438316