user5164720
user5164720

Reputation:

jQuery remove selected classes content from html

I need to grab the content of a #container, except the class .removethis

<div id="container">Lorem ipsum dolor sit amet, consectetur adipiscing elit, 
   <div class="removethis">sed</div> do 
   <div class="removethis">eiusmod</div> tempor incididunt ut labore et <b>dolore</b> magna aliqua. 
</div>

My attempt $('#container').remove('.removethis'); alert($('#container').html());

Upvotes: 0

Views: 24

Answers (1)

void
void

Reputation: 36703

1. If you want to remove .removethis from the html

Do $('.removethis').remove(); to remove .removethis elements.

$('.removethis').remove();
console.log($('#container').html())
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container">Lorem ipsum dolor sit amet, consectetur adipiscing elit,
  <div class="removethis">sed</div> do
  <div class="removethis">eiusmod</div> tempor incididunt ut labore et <b>dolore</b> magna aliqua. </div>

2. If you do not want to remove .removethis from the html but just while fetching the content

Or incase you do not actually want to remove the .removethis but just dont want it when fetching the HTML then take a clone of #container and remove .removethis from that.

var e = $('#container').clone();
$('.removethis', e).remove();
console.log(e.html());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container">Lorem ipsum dolor sit amet, consectetur adipiscing elit,
  <div class="removethis">sed</div> do
  <div class="removethis">eiusmod</div> tempor incididunt ut labore et <b>dolore</b> magna aliqua. </div>

Upvotes: 2

Related Questions