-1

I have the following HTML:

<div id = "id1">
    "some text 1"<br>
    "more text 2" <br>
    "name" <br>
    <li> "list 1"</li>
    "THEend"
    <div class="class1" style="margin: 0px; padding: 0px;">
      &nbsp;&nbsp;
    </div>
</div>

And the following JavaScript:

var contents  = document.getElementsById("id1").innerHTML

Currently, the result of contents includes the text from both id1 and class1 -- but I want to eliminate the contents of class1 entirely.

How should I proceed?

Casey Falk
  • 2,617
  • 1
  • 18
  • 29

1 Answers1

0

Here is a Really primitive example:

var original_id1 = document.getElementById('id1'),
    cloned_id1 = original_id1.cloneNode(true); // deep clone

// find all ".class1" elements
var class1_elements = cloned_id1.getElementsByClassName('class1');
// remove .class1 elements from clone
for (var i = 0; i < class1_elements.length; i++){
  var c1 = class1_elements[i], p = c1.parentNode
  p.removeChild(c1);
}

// remaining HTML
alert(cloned_id1.innerHTML);

// remaining text content(s)
alert(cloned_id1.textContent);

exmaple

Brad Christie
  • 100,477
  • 16
  • 156
  • 200