I've got JQuery UI widget that appends DOM element to do the job. The widget removes this element in _destroy method so it will clean-up after itself.
Everything is good until I try to replace widget with some other widget. Original widget gets removed (destroy method will remove widgets custom element), but new widget is not appended.
Best description is code so here is example: http://jsfiddle.net/71xdxLvp/3/
The question is: How to replace my widget?
Note:
I know where the problem is, but i don't know how to solve it.
Problem is in jquery.js in method replaceWith - see comments in code:
return this.each(function() {
var next = this.nextSibling, // nextSibling is widgets custom element
parent = this.parentNode;
// Widget is removed and the widgets custom element is removed too
jQuery( this ).remove();
if ( next ) {
// New widget should be prepended to the widgets custom element, but
// the element is already removed from DOM.
// Method before() check this and won't append new widget
jQuery(next).before( value );
} else {
jQuery(parent).append( value );
}
});
Edit:
replaceWith is called by third-party framework Apache Wicket
widget which creates DOM element is CodeMirror