2

I read in the link that we can have event handler when a jQuery UI widget is created using on.

https://api.jqueryui.com/jquery.widget/#event-create

$( ".selector" ).on( "widgetcreate", function( event, ui ) {} );

But when I tried in the JSFiddle like below, it doesn't work.

$.widget( "my.customwidget", {
    _create: function() {
        alert('Hi!');
    }
});
$('#widget').on('widgetcreate', function (event, ui) {
    alert('Hi again!');
});
$('#widget').customwidget();

The alert('Hi again!') doesn't get executed. Here's the fiddle.

Unless I do this.

$.widget( "my.customwidget", {
    _create: function() {
        this.element.trigger('widgetcreate');
    }
});

What did I do wrong?

6
  • What is my here? is that any element? Commented Feb 11, 2016 at 3:48
  • It's supposed to be namespace according to jQuery UI Commented Feb 11, 2016 at 4:09
  • you didn't include jquery nor jquery-ui Commented Feb 11, 2016 at 5:51
  • Your fiddle works perfectly! LOL Commented Feb 11, 2016 at 10:05
  • The fiddle here: jsfiddle.net/petrabarus/5n5rqxkt is showing the alert. Commented Feb 11, 2016 at 10:06

1 Answer 1

4

The events are bound using the name of the widget itself, ie. "customwidget". Due to the inheritance model, you only have customwidget metadata, but not widget metadata. In your example, the name of the event is thus actually customwidgetcreate:

$('#widget').on('customwidgetcreate', function (event, ui) {
    alert('Hi again!');
});

Here's the updated fiddle: https://jsfiddle.net/2z06faLn/

There was a related question on a similar topic a while ago: Why is $(...).widget undefined?

Sign up to request clarification or add additional context in comments.

1 Comment

And where in the documentation states this? I just want to know whether I missed some paragraphs or pages in the documentation..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.