views:

30

answers:

2

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

+1  A: 

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();
}
Sarfraz
thanks that works great!
Jamie Taylor
@Jamie Taylor: Welcome :)
Sarfraz
+1  A: 
if($('.propdata').length == 1)
    $('.topbook').hide();
Tomasz Wysocki