'Find count of text areas with a particular attribute using Jquery

In my Rails app, in a form, I have several text areas. The attribute display:none is specified for some text areas. How to get count of text areas with the attribute display:none using JQuery.



Solution 1:[1]

You can use the :hidden selector:

var hiddenCount = $('textarea:hidden').length;

Additional Notes:

Because :hidden is a jQuery extension and not part of the CSS specification, queries using :hidden cannot take advantage of the performance boost provided by the native DOM querySelectorAll() method. To achieve the best performance when using :hidden to select elements, first select the elements using a pure CSS selector, then use .filter(":hidden").

So, for best performance:

var hiddenCount = $('textarea').filter(':hidden').length;

Solution 2:[2]

var count = $('textarea').filter(function() {
    return $(this).css('display') == 'none';
}).length;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 billyonecan
Solution 2 Simon