title: How can I tell if a jQuery node is at the beginning of its parent node?
body: <p>Given the following HTML:</p>
  
  <pre><code>&lt;p&gt;&lt;img id="one" alt="at beginning, return true" /&gt;Some Text&lt;/p&gt;
  &lt;p&gt;Some &lt;img id="two" alt="in middle, return false" /&gt;Text&lt;/p&gt;
  &lt;p&gt;Some Text&lt;img id="three" alt="at end, return false" /&gt;&lt;/p&gt;
  </code></pre>
  
  <p>How would I be able to tell that <code>$("img#one")</code> is at the beginning of its parent node?</p>
  
  <p>Ideally what I'm trying to do is this:</p>
  
  <pre><code>$("p&gt;img").each(function () {
      var $this = $(this);
      var $parent = $this.parent();
      if ("$this is at the beginning of $parent.html()") {
          $parent.before($this);
      } else {
          $parent.after($this);
      }
  });
  </code></pre>
  
  <p><strong>Edit:</strong> with <a href="https://stackoverflow.com/questions/1144384/how-can-i-tell-if-a-jquery-node-is-at-the-beginning-of-its-parent-node/1144418#1144418">sebasgo's help</a>, here's the final code and result:</p>
  
  <pre><code>$("p&gt;img").each(function () {
      var $this = $(this);
      var $parent = $this.parent();
      if (this == this.parentNode.firstChild) {
          $parent.before($this);
      } else {
          $parent.after($this);
      }
  });
  
  &lt;img id="one" alt="at beginning, return true" /&gt;
  &lt;p&gt;Some Text&lt;/p&gt;
  &lt;p&gt;Some Text&lt;/p&gt;
  &lt;img id="two" alt="in middle, return false" /&gt;
  &lt;p&gt;Some Text&lt;/p&gt;
  &lt;img id="three" alt="at end, return false" /&gt;
  </code></pre>