Reitrac Reitrac - 1 month ago 7
jQuery Question

Compare 2 values in foreach

For each day, I do another foreach to return css

top
value:

$('.day').each(function() {
$(this).find('.event').each(function() {
var top = $(this).css('top');
});
});


I would like to know if I can compare 2
top
value?

I need that because, if 2
top
value are equals, I add new class.

EDIT :

For example :

First event : 2017-08-07 at 8am

Second event : 2017-08-07 at 8am

Third event : 2017-08-07 at 2pm

In my website:

-8am is 'top: 50px'
-2 pm is 'top: 600px'

If first-event->top-position == second-event->top-position


I hope, this will help you.

Answer Source

Here you can compare the top position of .day with the top position of its children, hoping this is what you meant.

Edited to compare top position of event compared to previous event for that day

var pos;
$('.day').each(function(__idx, __el) { 
  pos = false;    
  $(__el).find('.event').each(function(_idx, _el) {
       if(pos){
           if($(_el).position().top === pos){
               //do your thing here
           }
      }
      pos = $(_el).position().top;
  });
});