title: jquery remove html elements inside variable (jquery object)
body: <p>Inside HTML I have this DIV:</p>
  
  <pre><code>&lt;div class="teaser"&gt;
    &lt;img src="thumb1.jpg"&gt;&lt;img src="thumb2.jpg"&gt;&lt;br&gt;  // usually one thumb image; can be up to three
    some text goes here, sometimes not&lt;br&gt;
    &lt;a href="example.html"&gt;always a link here&lt;/a&gt;&lt;br&gt;
    and sometimes another line here
  &lt;/div&gt;
  </code></pre>
  
  <p>I need to capture the content of the DIV minus the first line (the thumbs) in a variable to pass on as title (caption) to a lightbox script.  However, in a day I've learned something about javascript &amp; jquery (both are my weak spots), yet nothing I found by searching the web, including several threads here on stackoverflow, did it, either throwing errors or still including the thumbs or removing the text, too, or even removing the thumbs from the DOM:</p>
  
  <pre><code>// takes the whole
  var teaser = $(".teaser");
  
  // TypeError: $(...).html(...).find is not a function
  var teaser = $(".teaser").find("img").remove();
  
  // IMGs removed from DOM
  var teaser = $(".teaser").find("img").remove();
  
  // IMGs still included; I think I understand why
  var teaser = $(".teaser").not("img");
  
  // ditto
  var teaser = $(".teaser").clone().not("img");
  
  // ditto:
  var teaser = $(".teaser");
  $(teaser).find("img").remove();
  
  // no teaser at all (everything removed, not just the IMGs)
  var teaser = $(".teaser");
  teaser = $(teaser).find("img").remove();
  
  // ditto
  var teaser = $(".teaser").clone().find("img").remove();
  </code></pre>
  
  <p>Changing the sequence in chainings didn't work either (though I may have missed one or another).</p>
  
  <p>This workaround seems to be ok:</p>
  
  <pre><code>var teaser = $(".teaser").html().replace(/(&lt;img.*?)+&lt;br[^&gt;]*?/i,"");
  </code></pre>
  
  <p>However, depending on regex implementation in browsers it might be shaky, so I'd rather have something more robust, particularly not getting the first line at all.</p>
  
  <p>TIA for any suggestion - hope I could make myself clear.</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>using <code>clone()</code> in conjunction with <code>children()</code> or <code>contents()</code> got me a step further.  However, both these will also remove the first text node, leaving two empty <code>BR</code> behind:</p>
  
  <pre><code>$("div.teaser").each(function() {
    var teaser = $(this).clone().children().not("img");
    var teaser = $(this).clone().contents().not("img");
  });
  </code></pre>
  
  <p>OTOH <code>filter("img")</code>, <code>find("img")</code> or <code>not("img")</code> followed by <code>remove()</code> removes everything but the IMGs, which is beyond my head except for <code>not()</code></p>
  
  <pre><code>var teaser = $(this).clone().find("img").remove();
  </code></pre>
  
  <p><strong>Edit 2:</strong></p>
  
  <p>To wrap things up:</p>
  
  <p>This is the solution @karaxuna suggested. It is important to re-assign the variable.</p>
  
  <pre><code>var teaser = $(this).clone();
  teaser.find("img,br:first").remove();
  teaser = $.trim(teaser.html());
  </code></pre>
  
  <p>While doing some further reading to better understand the logic behind the failures and the final solution I got a clue for doing it slightly more <em>jquerish</em>:</p>
  
  <pre><code>var teaser = $(this).clone();
  teaser = $.trim($("img,br:first",teaser).remove().end().html());
  </code></pre>
  
  <p>The clue is the scope, or context, added to the <code>$()</code> selector. Using <em>this</em> here is familiar enough, yet it never occurred to me to use the var. Also, <code>end()</code> will keep <code>remove()</code>from still going all the way to the DOM...</p>
  
  <p><code>$.trim</code> technically not necessary of course, HTML ignores empty lines anyway.</p>
  
  <p>So including the above workaround with regex there are at least three ways to go - kinda reminds me of Perl ;)</p>
  
  <p>Thanks to both @karaxuna and @Ravi for sharing their experience and ideas!</p>