0

I want to delete table element. Not from DOM. but from the string by jQuery as an element. Here is the code:

var content = $('<table cellspacing="0" cellpadding="0" border="0" class="testclass"><tbody><tr><td>test</td></tr></tbody></table>');
content.each(function(){
    $(this).remove();
});
console.log(content.prop('outerHTML'));

jQuery can remove if there will be more than 1 table. For example 2 or 3. And the last 1 will still. When there is only 1 table left in the string, .remove() function not working at all. There will be always still 1 final element that cannot be removed. The last one. But i need zero elements for example.

Payalord
  • 463
  • 2
  • 5
  • 16

1 Answers1

0

Not sure what you're trying to do, as your question isn't clear, but I just made this sample for you really quickly.

JS Bin

function removetable(table){
$(table).remove();

}

HTML:

<table id="table1" >
  <tr>
    <td>Table 1</td><td><button onclick="removetable('#table1');" >delete</button></td>
  </tr>
  </table>
  <table id="table2" >
  <tr>
    <td>Table 2</td><td><button onclick="removetable('#table2');" >delete</button></td>
  </tr>
  </table>
  <table id="table3" >
  <tr>
    <td>Table 3</td><td><button onclick="removetable('#table3');" >delete</button></td>
  </tr>
  </table>
  <table id="table4" >
  <tr>
    <td>Table 4</td><td><button onclick="removetable('#table4');" >delete</button></td>
  </tr>
  </table>
Gary Hayes
  • 1,728
  • 1
  • 15
  • 23
  • Thank you for trying to help me. I really appreciate that. I finally found an answer on [this topic](http://stackoverflow.com/questions/12109946/jquery-remove-tag-from-html-string) my problem was that i'm getting content from ajax, then i need to check does the content is legit. It is not a part of DOM yet. The content is some amount of tables, that's going one after another with the same classes. Sometimes there can be only 1 table. Sometimes a couple. Anyway if i used remove() function always 1 table will still. That's how jQuery remove work i guess. – Payalord Jun 15 '15 at 02:57