Selecting what is visible
jQuery has a special selector :visible
that selects all elements that occupy some space in the document. It's not exactly your requirement on display:none
, but it's more comprehensive and probably more useful.
See an example:
var qtd = $('#container').children(':visible').length;
console.log("Quantidade", qtd);
var itemTotalWidth = 0;
var qtd = $('#container').children(':visible').each(function() {
console.log($(this).outerWidth(true));
itemTotalWidth += $(this).outerWidth(true);
});
console.log("Largura total", itemTotalWidth);
#container span {
width: 20px;
margin: 0;
padding: 0;
display: inline-block;
}
.none {
display: none !important;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divid="container">
<span>bla</span>
<span style="display: none">bla</span>
<span style="width: 30px">bla</span>
<span class="none">bla</span>
<span>bla</span>
<span>bla</span>
</div>
Denying a CSS attribute
There are no selectors for CSS attributes in the same way that they exist for HTML attributes.
An alternative that works if you have full control over the styles and they are inline is to select by text comparison.
The following example uses a double selector :not([style*="display"]), :not([style*="none"])
, that is, it looks for both display
and none
values within the style
attribute in HTML. This no works if the attribute is declared elsewhere or changed dynamically.
var qtd = $('#container').children(':not([style*="display"]), :not([style*="none"])').length;
console.log("Quantidade", qtd);
var itemTotalWidth = 0;
var qtd = $('#container').children(':not([style*="display"]), :not([style*="none"])').each(function() {
console.log($(this).outerWidth(true));
itemTotalWidth += $(this).outerWidth(true);
});
console.log("Largura total", itemTotalWidth);
#container span {
width: 20px;
margin: 0;
padding: 0;
display: inline-block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divid="container">
<span>bla</span>
<span style="display: none">bla</span>
<span style="width: 30px">bla</span>
<span style="width: 50px; display: none">bla</span>
<span>bla</span>
<span>bla</span>
</div>
Alternatively, if you have full control over the styles and ensure there are no blanks in inline value, use a single :not([style*="display:none"])
selector.
However, I would consider these gambiarra techniques. It would be better to use jQuery :visible
or, even better, if you have control over the process, add different classes to the elements that are visible and invisible, so it is much simpler to perform operations on those elements without any hassle.