Is the selector returned ( #box1 , #box2 or #box3 ) if the condition worked, when refining the search using an enumeration, for example, $(".primer", "#box1, #box2, #box3") ?

I .each over elements using .each , for example, like this:

 $(".primer", "#box1, #box2, #box3").each(function(i){ любое условие, если элемент ему соответствует, сообщаем об этом }); 

Question. Does .each , in which of #box1 , or #box2 , or #box3 did the condition work if the search goes inside .each ?

If not, how can we figure this out, except for parent() , parents() and closest() , somehow more cleverly?

    2 answers 2

    Here is another option

     $(".primer", "#box1, #box2, #box3").each(function(i){ switch ($(this).attr('id')){ case 'box1': .... ...... } }); 

      The definition is simple: assign a class to the boxes and choose a parent with this class.

       $('.boxN .primer').each(function(){ var id = $(this).closest('.boxN').attr('id'); }); 

      And about returns or not - this is what you check for yourself. =)

      • I am doing this through .closest (see the question carefully), suggested that there is a more efficient way, so I asked - aleha29
      • It seems that nothing. Unless it is possible to invent the function. - ling