Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery: prevent plugin from re-using the code on exciting elements if extra content is inserted
    primarykey
    data
    text
    <p>I hope that the title is clear.</p> <p>So I have a complex big plugin, which transforms div's into sortable widgets, but I ran into some issue, if somebody ads a new div to the DOM the plugin has to run again to transform the newly added div into a widget, but that this means that it will run it over all already transformed div's. So how can you prevent this. Elements that are wrapped can be checked, so this isn't a big deal, but effects and other things will run several times(depending on how many divs are added to the DOM).</p> <p>I have made a simple idea how I am using the plugin.(sorry cant post the complete plugin here)</p> <p><strong>Not looking for a solution which contains a check on a added class, as this issue is the animations and such(like click events)</strong></p> <p>// the divs (the widgets)</p> <pre><code>&lt;div class="widget"&gt; &lt;div&gt;&lt;/div&gt; &lt;/div&gt; &lt;div class="widget"&gt; &lt;div&gt;&lt;/div&gt; &lt;/div&gt; ... ... ... </code></pre> <p>// the plugin(basic idea)</p> <pre><code>;(function($, window, document, undefined){ $.fn.plugin = function(options) { options = $.extend({}, $.fn.plugin.options, options); return this.each(function() { var obj = $(this); // for this example we wrap the div obj.find(options.widgets).wrap('&lt;div class="wrapped-widget"&gt;&lt;/div&gt;'); // add a link to the widget options.widgets.prepend('&lt;a href="#" class="link"&gt;link&lt;/a&gt;') // run the animation just once options.widgets.on('click', '.link', function(){ $(this).next().animate({height: 'toggle'}); }); }); }; $.fn.plugin.options = { widgets: '.widget', // etc }; })(jQuery, window, document); </code></pre> <p>// plugin call</p> <pre><code>$('body').plugin({widgets: '.widget'}); </code></pre> <p>// adding new div's to the DOM</p> <pre><code>$('#add').click(function(){ $('body').append('&lt;div class="widget"&gt;&lt;a href="#" class="link"&gt;link&lt;/a&gt;&lt;div&gt;&lt;/div&gt;&lt;/div&gt;'); $('body').plugin({widgets: '.widget'}); }); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload