find occurences of a class and if there is only one then hide with Jquery
I'm trying to hide a button depending on how many occurrences of a certain class there are.
I am dynamically putting data into a page and for each dynamic div i put in开发者_运维技巧 i assign it the class "propdata" I need to count how many occurrences there are of "propdata" and if there is only one occurrence i need to hide the button with a class of "topbook"
Hope that makes sense?
Thanks
Jamie
You can use the length
property like this:
// get total elements with class propdata
var cnt = $('.propdata').length;
// is there only one element with class propdata
if (cnt === 1){
// hide the element with class topbook
$('.topbook').hide();
}
Or you can make it shorter like this:
if ($('.propdata').length === 1){
$('.topbook').hide();
}
if($('.propdata').length == 1)
$('.topbook').hide();
精彩评论