I have a div with a table inside.
How can I hide the div if its value is null or empty and show it again if its value is not null or empty
<div class="container" id="main_container">
<table id="my_table" class="table table-wishlist">
<thead>
<tr>
<th>Name</th>
<th>Price</th>
</tr>
</thead>
<tbody class="wishlist-page-items">
<tr class="wishlist-page-item">
<td class="product-col">
<div class="lz-place"></div>
</td>
<td class="price-col">
<div class="lz-place"></div>
</td>
</tr>
</tbody>
</table>
</div>
I tried this function but I only managed to hide the div and not show it when the table is full.How could I achieve my goal either in javascript or jquery. Thanks
$(document).ready(function () {
if ($('#main_container').val() === "") {
//$('#main_container').hide();
document.getElementById("main_container").style.display = "none";
console.log("do not show")
} else {
//$('#main_container').hide();
document.getElementById("main_container").style.display = "block";
console.log("show")
}
});
3
Answers
.val()
is for getting the value of inputs, not the contents of DOM elements. Use.text()
to get the text in an element. And use.trim()
to remove all the surrounding whitespace.However, the text of
#main-container
will include the table headings, so you should only check the table body.Use
:empty
pseudo-class. Just simple example: