Reitrac Reitrac -3 years ago 130
jQuery Question

Compare 2 values in foreach

For each day, I do another foreach to return css


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

I would like to know if I can compare 2

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


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($(_el).position().top === pos){
               //do your thing here
      pos = $(_el).position().top;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download