Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing jQuery to control duplicate divs
    text
    copied!<p>I have two divs with the same content that have to be shown at different places on the same page. The problem is that when I want to show stuff in the container two I will effect the same divs in container one. Is there a way of making the jQuery treat each container as seperate entities? Do I have to use a jQuery plugin?</p> <p><strong>HTML</strong></p> <pre><code>&lt;body&gt; &lt;div class="container1"&gt; &lt;div class="main" style="display:none"&gt; Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. &lt;/div&gt; &lt;/div&gt; ... &lt;div class="container2"&gt; &lt;div class="main" style="display:none"&gt; Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat n ulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. &lt;/div&gt; &lt;div&gt; &lt;div class="button1"&gt;Display top main div&lt;/div&gt; &lt;div class="button2"&gt;Display bottom main div&lt;/div&gt; &lt;/body&gt; </code></pre> <p><strong>jQuery</strong></p> <pre><code>(function($) { $(".button1").click(function() { $('.main').slideToggle(500); }); $(".button2").click(function() { $('.main').slideToggle(500); }); })(jQuery); </code></pre> <p>// CAN I USE THIS PLUGIN (function($) {</p> <pre><code>var methods = { init: function(options) { return this.each(function() { var $this = $(this); }); } }; $.fn.plugin = function(method) { if (methods[method]) { return methods[method].apply(this, Array.prototype.slice.call(arguments, 1)); } else if (typeof method === 'object' || !method) { return methods.init.apply(this, arguments); } else { $.error('Method ' + method + ' does not exist on jQuery.plugin'); } }; </code></pre> <p>})(jQuery); </p>
 

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