saj sta
saj sta

Reputation: 95

jQuery fire an event and receive it

I want to fire event globally and receive it anywhere in my application. Based on the event received, I want to perform some operations.

I have a multistep form when user click the next button, I want to trigger an event

function multiStep(){
    $('.body').trigger('stepchanged');

}

Now I want to receive stepchanged in another function and perform some operations.

if (stepchanged){
//do something
} 

How to receive the events? Thanks

Upvotes: 0

Views: 58

Answers (1)

Barmar
Barmar

Reputation: 782785

You need an event handler that sets the variable.

`$(".body").on("stepchanged", function() { 
    stepchanged = true;
})`

Although there's not really any need for the event handler, unless it does other things. You can just set the global variable in multistep().

Upvotes: 1

Related Questions