how to access the array of classes with jQuery? Catching the second class of an element

2

I'm listing the icons of a list in console.log() they are displayed, however I want to capture only the second classes of the icons in a variable, but I do not know how to access the second classes of the <i> tag with jQuery. I'm using this code to access the <i> tags that are in the <li>

clickAtual = $('li.card').children().find('i');

Below the list is displayed, however I want to access the second class from this list shown below.

    
asked by anonymous 24.07.2018 / 01:01

2 answers

1

No need to use .children() . .find() will already find everything (children, grandchildren, great-grandchildren, great-grandchildren, etc.):

$('li.card').find("i")

From here, using the .each() loop, you can do a split in the element classes and get the [1] (second class) index of each and save to an array. With the array mounted you can convert to string, iterate, do whatever you want:

var clickAtual = $('li.card').find("i");
var array = [];
clickAtual.each(function(i,e){
   var c = e.className.split(" ")[1];
   array.push(c);
});


console.log(array);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><ul><liclass="card">
      <i class="classe1 classe2 classe3"></i>
      <i class="classe1 classe21 classe3"></i>
      <i class="classe1 classe22 classe3"></i>
      <i class="classe1 classe23 classe3"></i>
   </li>
</ul>
    
24.07.2018 / 02:43
1

This way:

classeDesejada = $('li.card').children().find('i:eq(0)');

You pass% s to% desired position. See more about the eq-selector in the :eq() documentation.

    
24.07.2018 / 01:09