How to compare if the div with event click has a class similar to another selected div?

0

I have a set of div's with jQuery event click. I'm not sure how to compare the div's clicked if they have the same icon class. I have a div already selected, if the user clicks another div in the same container I want to apply another event if it has similar icons comparing the classes of the icons if they are the same.

Ex: Div1 selected has <i class="fa fa-bicycle"></i> Div2 when clicked should compare to div1 if it also has <i class="fa fa-bicycle"></i> but I do not know how to make that comparison in jQuery

    
asked by anonymous 06.08.2018 / 21:06

1 answer

1

You can do using hasClass in the click event of each tag i , for example;

$('i').on('click', function() {
  var icone = $(this);
  
  if(icone.hasClass('fa-bicycle') == true){
    console.log('Tem a classe');
  } else {
    console.log('Não tem a classe');
  }
})
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><div><iclass="fa fa-bicycle"></i></div>
<div><i class="fa fa-home"></i></div>
<div><i class="fa fa-bicycle"></i></div>
    
06.08.2018 / 21:18