How to cancel change event for accordion controls
Asked Answered
R

6

8
$("#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?

Robb answered 1/12, 2010 at 11:18 Comment(1)
this cancel event but action still workingRobb
G
5

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

Gash answered 28/6, 2011 at 14:57 Comment(1)
This works because return false is equivalent to calling e.preventDefault(); and e.stopPropagation();Darrendarrey
D
3

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.

Deepseated answered 16/3, 2012 at 19:37 Comment(0)
B
2

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

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

    return false;
}
Boon answered 8/4, 2014 at 5:50 Comment(0)
T
1
event.preventDefault();
event.stopPropagate();
Telephotography answered 1/12, 2010 at 12:53 Comment(1)
stopPropagation(), not "stopPropogate()"Darrendarrey
S
0

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. :)

Sochi answered 4/9, 2014 at 14:30 Comment(2)
"These handlers, therefore, may prevent the delegated handler from triggering by calling event.stopPropagation() or returning false." - api.jquery.com/event.stoppropagationPublius
This actually doesn't work. It was my first thought too.Darrendarrey
D
0

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.

Darrendarrey answered 21/10, 2016 at 6:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.