3

Using $('div#top_container').html(), I get the following as an example:

<div class="top" id="top_container">
    <div class="example">First</div>
    <div class="example">Second</div>
</div>

giving...

<div class="example">First</div>
<div class="example">Second</div>

Here, using .replace(), I want to replace <div class="example"> with *%^% (random set of characters) and remove </div>:

var content = $('div#top_container').html();                
var clean_1 = content.replace('<div class="example">','*%^%'); //add $*!#$
var clean_2 = clean_1.replace('</div>',' '); //remove </div>

giving...

console.log(clean_2); --> *%^%First*%^%Second

Now, the number of example div elements can vary and I need to first find out how to target them all. Also is there a cleaner way to target both <div class="example"> and </div> at the same time?

EDIT:

I am not looking to change the html itself, but rather have the edited version as a variable that I can do stuff with (such as send it to php via ajax).

How would I do this?

1
  • I am not sure about your specific requirement of replace, but instead of replace its better to get the div's values (e.g. First, Second etc.) and concat them using '*%^%' Commented Dec 21, 2016 at 8:49

3 Answers 3

5

Use replaceWith() method with a callback and generate prefered text string by getting text content using text() method.

$('div.example').replaceWith(function(v) {
  return '%^%' + $(this).text();
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  <div class="example">First</div>
  <div class="example">Second</div>
</div>


UPDATE: If you don't want to update the original element then use clone() and do the remaining thinks on the cloned element.

var res = $('#parent')
  // clone element
  .clone()
  // get element with `example` class
  .find('.example')
  // update content
  .replaceWith(function(v) {
    return '%^%' + $(this).text();
  })
  // back to previos selector and get html content
  .end().html();

console.log(res)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="parent">
  <div class="example">First</div>
  <div class="example">Second</div>
</div>

Sign up to request clarification or add additional context in comments.

3 Comments

I just noticed that .replaceWith changes the element itself. I am looking to have it as a variable and not alter the html (I should have been more clear in the question). I will update the question.
Sorry for the confusion.
Thank you =) Appreciate it!
2

Create one prototype like :

String.prototype.replaceAll = function (toReplace, replaceWith){
    return this.split(toReplace).join(replaceWith);
}

and your jquery code be like :

  $("div#top_container").each(function( i ) {debugger;
    console.log(this.innerHTML.replaceAll('<div class="example">','*%^%').replaceAll('</div>',' ');)
  });

Comments

2

You can use replaceWith function

$(function () {
   $(".example").replaceWith(function(){
     return "%^%"+$(this).text();
    });
});

You can make a clone of container if you don't want to change original div.

var html="";
$(function () {
 var newHtml = $("#top_container").clone();
 $(newHtml).find(".example").replaceWith(function () {
   html+= "%^%" + $(this).text();
   });
});

console.log(html);

1 Comment

I wasn't clear in my question. I updated my question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.