title: Own jQuery plugin only working on first selector
body: <p>I've had a go at writing my first plugin. It is a simple jQuery plugin that reorders the DOM based on screen width. If the plugin is used on a single selector, like <code>$("#box3").domorder();</code> it works as expected. </p>
  
  <p>However, if multiple selectors are used to call the function, or the function is called multiple times, the function only works once, on the first selector it comes to.</p>
  
  <p>I have the <code>each(function(){})</code> iterating over each selector, but I'm obviously missing something.</p>
  
  <p><a href="http://jsfiddle.net/L385U/" rel="nofollow">jsFiddle</a></p>
  
  <pre><code>(function($) {
      "use strict";
  
      $.fn.domorder = function(options) {
  
          var settings = {
              breakpoint : 960,
              targetContainer : $(this).parent(),
              targetPosition : "start"
          };
  
          if (options) {
              $.extend(settings, options);
          }
  
          return this.each(function(i, el) {
              /* remember selector's original order position */
              var originalLocation = {};
              if ($(el).prev().length) {
                  /* not originally first child */
                  originalLocation.prev = $(el).prev()[0];
              } else {
                  /* originally a first child */
                  originalLocation.parent = $(el).parent()[0];
              }
  
              var initiatedomorder = function() {
                  var winW = $(window).width();
  
                  if (winW &lt; settings.breakpoint &amp;&amp; !$("body").hasClass("domorder-reordered")) {
                      /* dom the order of the item */
  
                      if (settings.targetPosition === "start") {
                          $(el).prependTo(settings.targetContainer[0]);
                      } else {
                          $(el).appendTo(settings.targetContainer[0]);
                      }
                      $("body").addClass("domorder-reordered");
                  } else if (winW &gt;= settings.breakpoint &amp;&amp; $("body").hasClass("domorder-reordered")) {
                      /* return the reordered item back into the orignal flow */
                      if (originalLocation.parent) {
                          /* element was a first child */
                          $(originalLocation.parent).prepend(el);
                      } else {
                          /* element was not a first child */
                          /* add a line break to preserve inline-block spacing */
                          $(originalLocation.prev).after(el).after("\n");
                      }
                      $("body").removeClass("domorder-reordered");
                  }
              };
  
              initiatedomorder();
  
              $(window).resize(function() {
                  initiatedomorder();
              });
          });
      };
  }(jQuery));
  </code></pre>