Search code examples
javascriptjqueryhtmlhtml2canvas

Extract body element without a particular child


Is there a way to extract body element without a particular child element in it?

For example, if I have:

<body>
  <div id="id1" class="class1" />
  <div id="id2" class="class2" />
</body>

, what I need to be extracted is:

<body>
  <div id="id1" class="class1" />
</body>

Actually, I intend to use html2canvas library to make canvas element from a HTML code, but I don't want to include all body children in a canvas element.


Solution

  • If you retrieve a parent element then you also have to take all of its children too. A possible workaround in this case would be to select the body, clone it and then remove the unwanted child element, something like this:

    var $bodyClone = $('body').clone();
    $bodyClone.find('#id2').remove();
    // use $bodyClone as needed here...