Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

jquery

Sorry for real stupid question. But it does not work either way.

<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.

Thanks.

like image 712
Alex G Avatar asked Feb 04 '12 22:02

Alex G


1 Answers

This should do it:

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

var html = clone.html();
like image 130
Richard Dalton Avatar answered Oct 13 '22 17:10

Richard Dalton