How can I get all tbody > tr
that doesn't have this style attribute: display: none
?
Answer: Use the jQuery :visible Selector You can use the jQuery :visible selector to check whether an element is visible in the layout or not. This selector will also select the elements with visibility: hidden; or opacity: 0; , because they preserve space in the layout even they are not visible to the eye.
Actually there's a CSS3 solution to select elements that doesn't have a display:none style, or given an explicit style property: *:not([style*="display: none"]) button{ ... } It may be a solution, but not at all a good recommendation on standard as you are using inline styling.
The accepted answer works, and is very useful, but not technically what the OP directly asked.
I'm splitting hairs, admittedly, but I needed to find only tr
elements which literally did not contain display: none
within their style
attribute, because the parent element might be hidden, thus returning no elements.
So I wrote the following:
var $trWithoutDisplayNone = $('tbody > tr:not([style*="display: none"])');
Update:
The original code will select an array of all tr
s on a page with no style attribute containing "display: none"
.
A more efficient and specific way would be to target the table itself.
HTML:
<table id="tableID">
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
</tr>
<tr style="background: grey; display: none;">
<td>5</td>
<td>6</td>
<td>7</td>
<td>8</td>
</tr>
<tr>
<td>9</td>
<td>10</td>
<td>11</td>
<td>12</td>
</tr>
</table>
JavaScript/jQuery:
<script>
function getDisplayedRows($targetTable) {
return $targetTable.find('tr:not([style*="display: none"])');
}
$(function() { //shortcut for document ready
var $table = $("#tableID"), //get table by selector
$visibleRows = getDisplayedRows($table); //run function to get rows without display: none
});
</script>
$("tbody > tr:visible")
Should do it, by using the :visible
selector.
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