How to count the number of hidden divs of one class with jquery

I have a dynamic form that I wrote in rails. I want to be sure that the user can add no more than five links.

I start with two links, and I have another link that allows the user to add another field. I also have a link next to the links, which allows the user to remove the field that sets the hidden field, and then hide the field using the slideUp (); function.

I want to know if there are 5 fields on the screen that the user hopes to present.

Here is what I'm using right now - it's just counting all divs with that class name.

if($(".classname").length <5){
//create element dynamically
}

I want to check if "style =" display is displayed: none; "" How can i do this?

+3
source share
1 answer

Use the selector :hidden:

if ($(".classname:hidden").length < 5) {
    //create element dynamically
}

This will return any element with this class that will not be accessible to the user. If you just want to check display:none, use filter():

$(".classname").filter(function () {
    return $(this).css("display") == "none";
});
+9
source

All Articles