ajax - jQuery plugin, unbind elements and rerun the plugin -
i'm making simple plugin activates when hyperlinks clicked example:
// actives plugin anchor class mylink jquery('a.mylink').c_podsystem(); // plugin (function($){ $.fn.c_podsystem = function(opt) { var opt = $.extend(opt); return this.each(function() { jquery(this).click(function(e){ // } }); }; })(jquery);
in instances need rerun plugin after ajax success (so anchors might returned in ajax class mylink work plugin).
however, if run jquery('a.mylink').c_podsystem(); in ajax callback, reruns items on page binded, causes plugin run twice.
so combat need unbind elements affected return this.each()
then rerun jquery('a.mylink').c_podsystem() actives anchors on page class mylink from fresh dom.
how unbind affected elements, can rerun plugin?
would possible adjust plugin use .live()?
attach handler event elements match current selector, , in future.
using live rework plugin following:
(function($) { $.fn.c_podsystem = function(opt) { var opt = $.extend(opt); jquery(this).live("click", function(e) { $("#result").append($(this).text()); }); } })(jquery);
bind existing a.mylink
jquery('a.mylink').c_podsystem();
append new link dom
$("#links").append($("<a>after plugin ran</a>").addclass("mylink"));
by using live click event handler still function intended plugin.
example on jsfiddle
Comments
Post a Comment