Given the following table:
<table id="incidents">
<thead>
<tr>
<th></th>
</tr>
</thead>
<tbody>
</tbody>
</table>
Using jQuery the following evaluate as false
:
$("#incidents tbody").is(":empty")
I would like to use CSS to set the content to be a message:
#incidents tbody:empty {
content: "<tr>message foo</tr>";
}
Can the :empty
selector be applied to tbody?
The is() method is used to check if one of the selected elements matches to the selector element. This method can be used on this element to test if it is empty by using “:empty” selector. The “:empty” selector is used to select all the elements that have no children.
The <tbody> tag is used to group the body content in an HTML table. The <tbody> element is used in conjunction with the <thead> and <tfoot> elements to specify each part of a table (body, header, footer). Browsers can use these elements to enable scrolling of the table body independently of the header and footer.
The <tbody> element is not a required child element for a parent <table> element to graphically render. However, it must be present, if the parent <table> element has a <thead> , a <tfoot> or another <tbody> element as a child.
No, unfortunately the indentation and newlines within tbody
prevent it from ever matching :empty
in this case. This applies both to CSS and jQuery, where as far as I know :empty
behaves identically, as well as jQuery's :parent
, which is equivalent to :not(:empty)
(with a very poor choice of name).
Furthermore, you cannot use CSS content
to add elements to the DOM. You'll need to do this in jQuery or the server side: check if tbody
has any rows and if not, add that row.
If you go with jQuery, you can use this:
var tbody = $("#incidents tbody");
if (tbody.children().length == 0) {
tbody.html("<tr>message foo</tr>");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With