How to get html () from an element excluding another element?

Sorry for the real stupid question. But this does not work anyway.

<html>
<head>
<script src='js/jquery.js' type='text/javascript'></script>
<script type='text/javascript'>
  $(document).ready(function() {
    var htmlcontent = $('.content').not('.dontgrab').html();
    alert(htmlcontent); // returns EVERYTHING
  });
</script>
</head>
<body>
<div class='content'>
  BEFORE
  <div class='dontgrab'>DON'T GRAB</div>
  AFTER
</div>
</body>
</html>

Tried $ (". Content *: not ('. Dontgrab')"). html (); // returns NULL

Please, help.

Thank.

+5
source share
2 answers

This should do it:

var clone = $('div.content').clone();
clone.find('.dontgrab').remove();

var html = clone.html();
+10
source

I think what you are trying to do:

var htmlcontent = $('.content').remove(".dontgrab").html();

You can use .not () if the situation were like this:

var htmlcontent = $('div').not(".dontgrab").html();

<body>
<div class="content">
  BEFORE  

  AFTER
</div>
<div class="content dontgrab">DON'T GRAB</div>
</body> 

I do not think you can use .not for selected children elements. I hope this helps

-1
source

All Articles