title: jquery next() returning nothing
body: <p>I was trying to select everything between adjacent <code>&lt;p&gt;</code>'s, the number of "p" changes every time. And contents in between p tag-pairs could be nothing to anything. Some thing like this:</p>
  
  <pre><code>    &lt;p&gt;&lt;a href="x"&gt;...ABC...&lt;/a&gt;&lt;/p&gt;
  
      &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt; Beginning of what I want &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;
  
      &lt;fieldset&gt;...&lt;/fieldset&gt;
      &lt;font title="..."&gt;...&lt;/font&gt;
      sometext without any tag&lt;br&gt;
      &lt;a href="..."&gt;...&lt;/a&gt;
      //[0..N] more tags
  
      &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt; End of what I want &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;
  
      &lt;p&gt;&lt;a href="x+1"&gt;...ABC...&lt;/a&gt;&lt;/p&gt;
      [0..N] more "p"'s with similar pattern ("p" with random url in "a")
  </code></pre>
  
  <p>Update:</p>
  
  <p>I want to wrap those rogue codes (untagged text) into some div so that I can process them later. Like this:</p>
  
  <pre><code>&lt;div id="outer"&gt;
      &lt;div id="1"&gt;
          &lt;p&gt;&lt;a href="x"&gt;...ABC...&lt;/a&gt;&lt;/p&gt;
  
      &lt;! Beginning of what I want &gt;
  
          &lt;fieldset&gt;...&lt;/fieldset&gt;
          &lt;font title="..."&gt;...&lt;/font&gt;
          sometext without any tag&lt;br&gt;
          &lt;a href="..."&gt;...&lt;/a&gt;
          //[0..N] more tags
  
      &lt;! End of what I want &gt;
      &lt;/div&gt;
      &lt;div id="2"&gt;
          &lt;p&gt;&lt;a href="x+1"&gt;...ABC...&lt;/a&gt;&lt;/p&gt;
      &lt;/div&gt;
      &lt;div id="3"&gt;
          //something or nothing
      &lt;/div&gt;
      //something or nothing
  &lt;/div&gt;
  </code></pre>
  
  <p>In order to do that, I had to this code because there is some text without any tags around it:</p>
  
  <pre><code>    var ps = $("p:contains('ABC')");
      ps.each(function(){
          if(!($(this).next()[0])){
              return true;
          }
          var me = $(this);
          var pa = me.parent().contents();
          var nx = me.next("p:contains('ABC')"); //returns [] in this case
          var i0 = pa.index(me);
          var i1 = pa.index(nx);
          if (i1 &gt; i0) {
              var elements = pa.slice(i0, i1);
              elements.each(function(){
                  //Do something
              });
          }
      }); 
  </code></pre>
  
  <p>As marked in the code, the next() function would not return anything even if I change it to next("p"). But if I use me.next().next().next().next().next() I can select the next "p" tag. Why would this happen? How could I do it better?   </p>