Jeff Lowery
Jeff Lowery

Reputation: 2597

JQuery custom event not firing/caught

I'm loading some scripts dynamically as follows:

//fileA.js
$('body').on('LoginWidget', function() {
    $.cachedScript('/js/min/AddressWidgets.min.js').done(function() {
        extend(AddressBookWidget, ShippingAddressWidget);
        console.log("throwing event")
        $('body').trigger('ShippingAddressWidget loaded');
    });
});

and then:

//fileB.js
$(document).ready(
        function() {
            console.log("catching event")
            $('body').on('ShippingAddressWidget loaded', function() {
                // do stuff
            });
...

In the console, I see:

catching event
throwing event

But the event catcher is not triggered. jquery 2.1.2

Upvotes: 0

Views: 67

Answers (1)

adeneo
adeneo

Reputation: 318202

Event names can not contain spaces.

You have to either use an event name like

$('body').on('ShippingAddressWidgetLoaded', function() {...

or namespace the event

$('body').on('ShippingAddressWidget.loaded', function() {...

FIDDLE

Upvotes: 1

Related Questions