Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery get elements without display="none"

How can I get all tbody > tr that doesn't have this style attribute: display: none?

like image 286
Yekver Avatar asked Apr 23 '11 22:04

Yekver


People also ask

How check display is blocked or not in jQuery?

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.

How do I select display none in CSS?

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.


2 Answers

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 trs 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>
like image 56
Sean Kendle Avatar answered Sep 24 '22 11:09

Sean Kendle


$("tbody > tr:visible")

Should do it, by using the :visible selector.

like image 39
Håvard Avatar answered Sep 23 '22 11:09

Håvard