You're calling the widget in the wrong way. You want your element to "become" your widget, so you'd set your widget element like you would any other jQuery UI widget, ie. $('element').scrollable()
, .draggable()
, etc.
Inside your widget delcaration, you want to set all the listeners and events to each element that the widget is set to. So if you want something to fire on click of your widget element, you would set a click event to this.element
, referring to the current widget element, in this case your div.
The _init
function is only called once, like everyone else has said, as it initializes your widget. However, if you set it up correctly, the click event should fire on every click of your element. This should help.
HTML:
<div id="hello_world">Click Me</div>
JS:
$.widget("ui.myWidget", {
_init: function() {
//start
this._show();
var thisWidget = this;
this.options.handler.click(function(){
thisWidget._show();
});
},
_show: function(){
alert('Hello World???');
}
});
$('div#click_me').click(function(){
$('#id').myWidget({handler: $(this)});
});