I would like to create a jquery plugin for my website.
I will create events, and I would like to know what is the best way to deal with handlers.
My worries are that when the DOM html change due to an ajax query, we have to keep the events bind. After some researches I found that the best way is to use the .on() function, available in jquery.
Here is what I have tried :
jQuery.fn.wtpicker = function ( params ) {
var currentSelector = $(this);
$(document).on('click', currentSelector, function(event) {
alert('test');
});
return this;
}
But it does not work...
live() is deprecated, classic bind d.click() don't work if changing dynamically the HTML with ajax queries... well I'm lost.
I am supposed to specify the dom of the selector, like "#whatever div.myclass" in the second argument. But as it is a plugin I can't know it.
To make it work I'm forced to write :
jQuery.fn.wtpicker = function ( params ) {
var currentSelector = $(this);
$(document).on('click', '#whatever div.myclass' , function(event) {
alert('test');
});
return this;
}
But it's not really efficient if I wan't another selector.
Is that possible to retrieve witch DOM selector was used in the plugin himself ? I don't want to pass a param with the dom. It's not really a good method.
Thanks a lot