javascript - Sortover and sortout events not triggered after element is replaced -
the middle column should change width when dragging element on it. work until click on replace button replaces html of middle column. events stop being triggered. far know .on() should able handle such cases, or wrong?
this simple demo demonstrates issue: https://jsfiddle.net/shuetvyj/3/
html:
<div class="members-column"> <div class="sort"> 1 </div> <div class="sort"> 1 </div> </div> <div id="groupb" class="members-column"> </div> <div class="members-column"> <div class="sort"> 2 </div> </div> <a href="" id="replace">replace groupb</a> javascript:
$(".members-column").sortable({ items: ".sort", connectwith: ".members-column" }); $("#groupb").on("sortover", function() { console.log('overb'); $('#groupb').css('min-width','80px'); }); $("#groupb").on("sortout", function() { console.log('outb'); $('#groupb').css('min-width',''); }); $("#replace").on("click", function(e) { e.preventdefault(); $("#groupb").replacewith('<div id="groupb" class="members-column"></div>'); });
when replace html, listeners had attached old html not reattached new html.
as result, handlers no longer called. solve this, attach listeners new html when have replaced it.
(jquery solved while live instead of on, automatically attach listeners after dom changes) http://api.jquery.com/live/
but live() has been deprecated since jquery 1.7 , removed in 1.9
additionally, removing column breaks sortable's behavior (since not store selector result of selector when first called, , retains reference div removed dom.
in short ; not remove column div once have sortable listening it, clear or replace contents
Comments
Post a Comment