开发者

find occurences of a class and if there is only one then hide with Jquery

开发者 https://www.devze.com 2023-01-14 09:37 出处:网络
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

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();
0

精彩评论

暂无评论...
验证码 换一张
取 消