Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

:empty selector on tbody tag

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?

like image 786
Darbio Avatar asked Jul 30 '13 05:07

Darbio


People also ask

How check Tbody is empty or not in jquery?

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.

What is the Tbody HTML tag?

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.

Is Tbody tag required?

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.


1 Answers

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>");
}
like image 83
BoltClock Avatar answered Oct 02 '22 07:10

BoltClock