scorpio1441
scorpio1441

Reputation: 3088

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

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.

Upvotes: 5

Views: 3467

Answers (2)

Mantas
Mantas

Reputation: 3025

I think you are trying to do:

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

You could use .not() if the situation was like:

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

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

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

I dont think you can use .not for selected elements children. I hope it helps

Upvotes: -1

Richard Dalton
Richard Dalton

Reputation: 35793

This should do it:

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

var html = clone.html();

Upvotes: 12

Related Questions