Simbian
Simbian

Reputation: 21823

How to cancel change event for accordion controls

$("#accordion").accordion({
    change: function (event, ui) {
        alert('event have to be changed')
    },
    changestart: function (event, ui) {
       return false;
    }
});

Is it possible to cancel the change event?

Upvotes: 8

Views: 10628

Answers (6)

earl3s
earl3s

Reputation: 2373

What worked for me is calling:

function onChange(e, ui) {
    e.preventDefault();
    e.stopPropagation();
}

Returning false at the end of the function has the same affect because it's the equivalent of making those two function calls.

Upvotes: 0

Robee Shepherd
Robee Shepherd

Reputation: 9

Returning false does nothing, to cancel an event in jQueryUI you need to call preventDefault() on the event.

$("#accordion").accordion({
    changestart: function (event, ui) {
       event.preventDefault();
    }
});

This should work with all event methods, including autocomplete. Handy if you wish to include a few custom options in your autocomplete menu that you want someone to select but don't actually want to add to the input it is tied to. :)

Upvotes: 0

Dean Oakley
Dean Oakley

Reputation: 5636

As Martin said you can return false. See a code sample below.

...
eventname: function( event, ui ) {
    // Add your code here
    ...

    return false;
}

Upvotes: 2

Dave
Dave

Reputation: 4412

stopPropagation() is the key to stopping a jQueryUI action from processing, but it must be interrupted BEFORE the changestart event. For example, when the header item is actually clicked.

If this is a header/content pair in your accordion:

<h3 class='noexpand'>This is a dummy header</h3>
<div>This is dummy content</div>

Then this will prevent the accordion from expanding the div when the header is clicked:

$("h3.noexpand").click(function(e) {
    e.stopPropagation();
});

See jquery accordion prevent bubbling / allow default link action.

Upvotes: 3

Martin Wawrusch
Martin Wawrusch

Reputation: 790

I am not sure about change but for other events in jqueryui returning false cancels the event.

Upvotes: 5

Ali Tarhini
Ali Tarhini

Reputation: 5358

event.preventDefault();
event.stopPropagate();

Upvotes: 1

Related Questions