Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Dec 2011
    Posts
    41
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Delete an element and all following elements in a doc except closing tags?

    Assume I have a HTML document like this

    <html>
    ...
    <body>
    ...
    <div>
    ....
    <div>
    ...foobar....
    </div>
    ...
    </div>
    ...
    </body
    </html>

    Now I want to delete the first, innermost (<div>) element which contains the text string "foobar" AND all following elements.
    However the closing tags of the elements started above should be kept.

    How can I achieve this?

    Peter

  • #2
    Regular Coder Lerura's Avatar
    Join Date
    Aug 2005
    Location
    Denmark
    Posts
    947
    Thanks
    0
    Thanked 130 Times in 129 Posts
    asuming the div contains the documents last occurance of "foobar", try this:
    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title></title>
    <meta charset="windows-1252">
    <script>
    function Remove(Text){
     divs=document.getElementsByTagName('div');
     var elem,C=divs.length-1;
     while (elem==undefined && C>-1){
      if ((divs[C].innerHTML).indexOf(Text)!=-1){
       elem=divs[C];}
       C--;
      }
     if (elem){
      pdiv=elem.parentNode;
      while (pdiv.lastChild!=elem){
       pdiv.removeChild(pdiv.lastChild); // remove succeeding siblings
      }
      pdiv.removeChild(pdiv.lastChild); // remove itself
     }
    }
    </script>
    
    </head>
    <body>
      <div>
        <div>1</div>
        <div>2</div>
        <div>3</div>
        <div>4</div>
      </div>
      <div>
        <div>5</div>
        <div>6</div>
        <div>7</div>
        <div>8</div>
      </div>
      <div>
        <div>9</div>
        <div>foobar</div>
        <div>11</div>
        <div>12</div>
      </div>
      <div>
        <div>13</div>
        <div>14</div>
        <div>15</div>
        <div>16</div>
      </div>
    <input type="button" onclick="Remove('foobar')" value="Remove">
    
    </body>
    </html>

  • #3
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,436
    Thanks
    13
    Thanked 360 Times in 356 Posts
    However the closing tags of the elements started above should be kept.
    you cannot delete tags, you can only delete elements (or groups of elements). the tags in the HTML source code are only used by the browser’s HTML parser to find out what’s considered to be an element.

    cf. How Browsers Work: Behind the scenes of modern web browsers - HTML5 Rocks (an in-depth article about what happens when you load a web page)
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •