Delete that is empty (does not contain text) using javascript/jquery [duplicate]
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>
javascript jquery html
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.
add a comment |
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>
javascript jquery html
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 yourtds (or directly to yourtable)
– Federico klez Culloca
Nov 15 '18 at 16:30
please write full code
– Chickenturtle
Nov 15 '18 at 16:32
add a comment |
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>
javascript jquery html
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
javascript jquery html
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 yourtds (or directly to yourtable)
– Federico klez Culloca
Nov 15 '18 at 16:30
please write full code
– Chickenturtle
Nov 15 '18 at 16:32
add a comment |
Check this and apply it to yourtds (or directly to yourtable)
– 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
add a comment |
2 Answers
2
active
oldest
votes
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>
This only remove innertableand other table that hastrdoes not removed
– Mohammad
Nov 16 '18 at 14:33
add a comment |
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>
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
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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>
This only remove innertableand other table that hastrdoes not removed
– Mohammad
Nov 16 '18 at 14:33
add a comment |
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>
This only remove innertableand other table that hastrdoes not removed
– Mohammad
Nov 16 '18 at 14:33
add a comment |
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>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>answered Nov 15 '18 at 16:33
simonecoscisimonecosci
82658
82658
This only remove innertableand other table that hastrdoes not removed
– Mohammad
Nov 16 '18 at 14:33
add a comment |
This only remove innertableand other table that hastrdoes 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
add a comment |
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>
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
add a comment |
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>
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
add a comment |
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>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>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
add a comment |
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
add a comment |
Check this and apply it to your
tds (or directly to yourtable)– Federico klez Culloca
Nov 15 '18 at 16:30
please write full code
– Chickenturtle
Nov 15 '18 at 16:32