Delete that is empty (does not contain text) using javascript/jquery [duplicate]












-1
















This question already has an answer here:




  • Check if DOM element and children contains any text [JS or jQuery]

    5 answers




I have been trying to figure out a simple way to remove an HTML table if it is empty.



For instance, if the table looks like this:



<table>
<tbody>
<tr>
<td>
<table>
<tbody>

</tbody>
</table>
</td>
</tr>
</tbody>
</table>


I want to remove from the top level <table>










share|improve this question















marked as duplicate by freedomn-m, Community Nov 16 '18 at 9:30


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • Check this and apply it to your tds (or directly to your table)

    – Federico klez Culloca
    Nov 15 '18 at 16:30













  • please write full code

    – Chickenturtle
    Nov 15 '18 at 16:32
















-1
















This question already has an answer here:




  • Check if DOM element and children contains any text [JS or jQuery]

    5 answers




I have been trying to figure out a simple way to remove an HTML table if it is empty.



For instance, if the table looks like this:



<table>
<tbody>
<tr>
<td>
<table>
<tbody>

</tbody>
</table>
</td>
</tr>
</tbody>
</table>


I want to remove from the top level <table>










share|improve this question















marked as duplicate by freedomn-m, Community Nov 16 '18 at 9:30


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • Check this and apply it to your tds (or directly to your table)

    – Federico klez Culloca
    Nov 15 '18 at 16:30













  • please write full code

    – Chickenturtle
    Nov 15 '18 at 16:32














-1












-1








-1


0







This question already has an answer here:




  • Check if DOM element and children contains any text [JS or jQuery]

    5 answers




I have been trying to figure out a simple way to remove an HTML table if it is empty.



For instance, if the table looks like this:



<table>
<tbody>
<tr>
<td>
<table>
<tbody>

</tbody>
</table>
</td>
</tr>
</tbody>
</table>


I want to remove from the top level <table>










share|improve this question

















This question already has an answer here:




  • Check if DOM element and children contains any text [JS or jQuery]

    5 answers




I have been trying to figure out a simple way to remove an HTML table if it is empty.



For instance, if the table looks like this:



<table>
<tbody>
<tr>
<td>
<table>
<tbody>

</tbody>
</table>
</td>
</tr>
</tbody>
</table>


I want to remove from the top level <table>





This question already has an answer here:




  • Check if DOM element and children contains any text [JS or jQuery]

    5 answers








javascript jquery html






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 16:42









Mohammad

15.8k123664




15.8k123664










asked Nov 15 '18 at 16:26









Neil MorganNeil Morgan

196




196




marked as duplicate by freedomn-m, Community Nov 16 '18 at 9:30


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by freedomn-m, Community Nov 16 '18 at 9:30


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • Check this and apply it to your tds (or directly to your table)

    – Federico klez Culloca
    Nov 15 '18 at 16:30













  • please write full code

    – Chickenturtle
    Nov 15 '18 at 16:32



















  • Check this and apply it to your tds (or directly to your table)

    – Federico klez Culloca
    Nov 15 '18 at 16:30













  • please write full code

    – Chickenturtle
    Nov 15 '18 at 16:32

















Check this and apply it to your tds (or directly to your table)

– Federico klez Culloca
Nov 15 '18 at 16:30







Check this and apply it to your tds (or directly to your table)

– Federico klez Culloca
Nov 15 '18 at 16:30















please write full code

– Chickenturtle
Nov 15 '18 at 16:32





please write full code

– Chickenturtle
Nov 15 '18 at 16:32












2 Answers
2






active

oldest

votes


















0














try this






<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tbody>
<tr>
<td>
<table>
<tbody></tbody>
</table>
</td>
</tr>
</tbody>
</table>
<script>
$("tbody").filter(function(){
return $(this).children().length == 0;
}).closest("table").remove();
</script>








share|improve this answer
























  • This only remove inner table and other table that has tr does not removed

    – Mohammad
    Nov 16 '18 at 14:33



















2














You can use .filter() to filter tables. In function check that table has text content or not.






$('table').filter(function(){
return $(this).text().trim() == ''
}).remove();

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tbody>
<tr>
<td>
<table>
<tbody></tbody>
</table>
</td>
</tr>
</tbody>
</table>








share|improve this answer


























  • As per the answer linked in the comments to the question, this works because .text() also considers the element's descendants.

    – Federico klez Culloca
    Nov 15 '18 at 16:32













  • Could remove the outer table also if it is only a shell. Might be better to use $('table table')

    – charlietfl
    Nov 15 '18 at 16:34








  • 1





    @charlietfl OP says they want to remove the top level table, so this is ok

    – Federico klez Culloca
    Nov 15 '18 at 16:34











  • @FedericoklezCulloca depends on how you interpret "I want to remove from". I read as the opposite

    – charlietfl
    Nov 15 '18 at 16:36













  • @charlietfl I missed the "from". Whoopsie

    – Federico klez Culloca
    Nov 15 '18 at 16:38




















2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














try this






<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tbody>
<tr>
<td>
<table>
<tbody></tbody>
</table>
</td>
</tr>
</tbody>
</table>
<script>
$("tbody").filter(function(){
return $(this).children().length == 0;
}).closest("table").remove();
</script>








share|improve this answer
























  • This only remove inner table and other table that has tr does not removed

    – Mohammad
    Nov 16 '18 at 14:33
















0














try this






<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tbody>
<tr>
<td>
<table>
<tbody></tbody>
</table>
</td>
</tr>
</tbody>
</table>
<script>
$("tbody").filter(function(){
return $(this).children().length == 0;
}).closest("table").remove();
</script>








share|improve this answer
























  • This only remove inner table and other table that has tr does not removed

    – Mohammad
    Nov 16 '18 at 14:33














0












0








0







try this






<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tbody>
<tr>
<td>
<table>
<tbody></tbody>
</table>
</td>
</tr>
</tbody>
</table>
<script>
$("tbody").filter(function(){
return $(this).children().length == 0;
}).closest("table").remove();
</script>








share|improve this answer













try this






<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tbody>
<tr>
<td>
<table>
<tbody></tbody>
</table>
</td>
</tr>
</tbody>
</table>
<script>
$("tbody").filter(function(){
return $(this).children().length == 0;
}).closest("table").remove();
</script>








<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tbody>
<tr>
<td>
<table>
<tbody></tbody>
</table>
</td>
</tr>
</tbody>
</table>
<script>
$("tbody").filter(function(){
return $(this).children().length == 0;
}).closest("table").remove();
</script>





<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tbody>
<tr>
<td>
<table>
<tbody></tbody>
</table>
</td>
</tr>
</tbody>
</table>
<script>
$("tbody").filter(function(){
return $(this).children().length == 0;
}).closest("table").remove();
</script>






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 '18 at 16:33









simonecoscisimonecosci

82658




82658













  • This only remove inner table and other table that has tr does not removed

    – Mohammad
    Nov 16 '18 at 14:33



















  • This only remove inner table and other table that has tr does not removed

    – Mohammad
    Nov 16 '18 at 14:33

















This only remove inner table and other table that has tr does not removed

– Mohammad
Nov 16 '18 at 14:33





This only remove inner table and other table that has tr does not removed

– Mohammad
Nov 16 '18 at 14:33













2














You can use .filter() to filter tables. In function check that table has text content or not.






$('table').filter(function(){
return $(this).text().trim() == ''
}).remove();

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tbody>
<tr>
<td>
<table>
<tbody></tbody>
</table>
</td>
</tr>
</tbody>
</table>








share|improve this answer


























  • As per the answer linked in the comments to the question, this works because .text() also considers the element's descendants.

    – Federico klez Culloca
    Nov 15 '18 at 16:32













  • Could remove the outer table also if it is only a shell. Might be better to use $('table table')

    – charlietfl
    Nov 15 '18 at 16:34








  • 1





    @charlietfl OP says they want to remove the top level table, so this is ok

    – Federico klez Culloca
    Nov 15 '18 at 16:34











  • @FedericoklezCulloca depends on how you interpret "I want to remove from". I read as the opposite

    – charlietfl
    Nov 15 '18 at 16:36













  • @charlietfl I missed the "from". Whoopsie

    – Federico klez Culloca
    Nov 15 '18 at 16:38


















2














You can use .filter() to filter tables. In function check that table has text content or not.






$('table').filter(function(){
return $(this).text().trim() == ''
}).remove();

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tbody>
<tr>
<td>
<table>
<tbody></tbody>
</table>
</td>
</tr>
</tbody>
</table>








share|improve this answer


























  • As per the answer linked in the comments to the question, this works because .text() also considers the element's descendants.

    – Federico klez Culloca
    Nov 15 '18 at 16:32













  • Could remove the outer table also if it is only a shell. Might be better to use $('table table')

    – charlietfl
    Nov 15 '18 at 16:34








  • 1





    @charlietfl OP says they want to remove the top level table, so this is ok

    – Federico klez Culloca
    Nov 15 '18 at 16:34











  • @FedericoklezCulloca depends on how you interpret "I want to remove from". I read as the opposite

    – charlietfl
    Nov 15 '18 at 16:36













  • @charlietfl I missed the "from". Whoopsie

    – Federico klez Culloca
    Nov 15 '18 at 16:38
















2












2








2







You can use .filter() to filter tables. In function check that table has text content or not.






$('table').filter(function(){
return $(this).text().trim() == ''
}).remove();

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tbody>
<tr>
<td>
<table>
<tbody></tbody>
</table>
</td>
</tr>
</tbody>
</table>








share|improve this answer















You can use .filter() to filter tables. In function check that table has text content or not.






$('table').filter(function(){
return $(this).text().trim() == ''
}).remove();

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tbody>
<tr>
<td>
<table>
<tbody></tbody>
</table>
</td>
</tr>
</tbody>
</table>








$('table').filter(function(){
return $(this).text().trim() == ''
}).remove();

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tbody>
<tr>
<td>
<table>
<tbody></tbody>
</table>
</td>
</tr>
</tbody>
</table>





$('table').filter(function(){
return $(this).text().trim() == ''
}).remove();

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tbody>
<tr>
<td>
<table>
<tbody></tbody>
</table>
</td>
</tr>
</tbody>
</table>






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 15 '18 at 16:32

























answered Nov 15 '18 at 16:31









MohammadMohammad

15.8k123664




15.8k123664













  • As per the answer linked in the comments to the question, this works because .text() also considers the element's descendants.

    – Federico klez Culloca
    Nov 15 '18 at 16:32













  • Could remove the outer table also if it is only a shell. Might be better to use $('table table')

    – charlietfl
    Nov 15 '18 at 16:34








  • 1





    @charlietfl OP says they want to remove the top level table, so this is ok

    – Federico klez Culloca
    Nov 15 '18 at 16:34











  • @FedericoklezCulloca depends on how you interpret "I want to remove from". I read as the opposite

    – charlietfl
    Nov 15 '18 at 16:36













  • @charlietfl I missed the "from". Whoopsie

    – Federico klez Culloca
    Nov 15 '18 at 16:38





















  • As per the answer linked in the comments to the question, this works because .text() also considers the element's descendants.

    – Federico klez Culloca
    Nov 15 '18 at 16:32













  • Could remove the outer table also if it is only a shell. Might be better to use $('table table')

    – charlietfl
    Nov 15 '18 at 16:34








  • 1





    @charlietfl OP says they want to remove the top level table, so this is ok

    – Federico klez Culloca
    Nov 15 '18 at 16:34











  • @FedericoklezCulloca depends on how you interpret "I want to remove from". I read as the opposite

    – charlietfl
    Nov 15 '18 at 16:36













  • @charlietfl I missed the "from". Whoopsie

    – Federico klez Culloca
    Nov 15 '18 at 16:38



















As per the answer linked in the comments to the question, this works because .text() also considers the element's descendants.

– Federico klez Culloca
Nov 15 '18 at 16:32







As per the answer linked in the comments to the question, this works because .text() also considers the element's descendants.

– Federico klez Culloca
Nov 15 '18 at 16:32















Could remove the outer table also if it is only a shell. Might be better to use $('table table')

– charlietfl
Nov 15 '18 at 16:34







Could remove the outer table also if it is only a shell. Might be better to use $('table table')

– charlietfl
Nov 15 '18 at 16:34






1




1





@charlietfl OP says they want to remove the top level table, so this is ok

– Federico klez Culloca
Nov 15 '18 at 16:34





@charlietfl OP says they want to remove the top level table, so this is ok

– Federico klez Culloca
Nov 15 '18 at 16:34













@FedericoklezCulloca depends on how you interpret "I want to remove from". I read as the opposite

– charlietfl
Nov 15 '18 at 16:36







@FedericoklezCulloca depends on how you interpret "I want to remove from". I read as the opposite

– charlietfl
Nov 15 '18 at 16:36















@charlietfl I missed the "from". Whoopsie

– Federico klez Culloca
Nov 15 '18 at 16:38







@charlietfl I missed the "from". Whoopsie

– Federico klez Culloca
Nov 15 '18 at 16:38





Popular posts from this blog

Bressuire

Vorschmack

Quarantine