How do I count the number of hidden divs of a certain class with jquery
I have a dynamic form that I've written in rails. I want to be sure that a 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 that allows the user to remove a field, which sets a hidd开发者_如何学运维en field and then hides the field with slideUp();.
I want to know if there are 5 fields on the screen that the user is hoping to submit.
Here's what I'm currently using - this just counts all of the divs with that classname.
if($(".classname").length <5){
//create element dynamically
}
I want to check if "style='display: none;'" How might I do that?
Use the :hidden
selector:
if ($(".classname:hidden").length < 5) {
//create element dynamically
}
This will return any element with that class which is not viewable to the user. If you just want to check for display:none
, then use filter()
:
$(".classname").filter(function () {
return $(this).css("display") == "none";
});
You can try like this
$('.classname:not([style*="display: none"])').length
精彩评论