replaceWith() does not work like expected

replaceWith() does not work like expected

Hi there,

This thing is driving me nuts. Tried all kinds of things to replace all img tags within a html string.

So I have this html on my page:

  1. <section><div class="content">Here is the html that has to be checked for IMG</div><div class="replyinfo"><span>25 Jan 2015, 13:18</span><a href="#" class="reply">Reply</a></div></section>
  2. <section><div class="content">Here is the html that has to be checked for IMG</div><div class="replyinfo"><span>26 Jan 2015, 15:02</span><a href="#" class="reply">Reply</a></div></section>
  3. <section><div class="content">Here is the html that has to be checked for IMG</div><div class="replyinfo"><span>27 Jan 2015, 08:43</span><a href="#" class="reply">Reply</a></div></section>
Then I have this jQuery:

  1. $(".reply").click(function(e) {
  2. e.preventDefault();
  3. var cmt = $(this).parents().find(".content").html();
  4. }
This will get the content of the content div (Don't know why closest() doesnt work but ok).

Now I want all image tags to be replaced inside the cmt string (Not on the page but only to process its output)...
I tried:

  1. var cmt2 = $('cmt').find("img").replaceWith("test");
and
  1. var cmt2 = $('cmt img').replaceWith("test");
But it all doesnt work and gives null or undefined when I print out the cmt2 output... :(

Can someone help me with this one?