Evgeniy  Volkov Evgeniy Volkov - 1 month ago 7
Javascript Question

How to select in each row the last element with certain class?

In each row there are some cells containing class .meow.
How do I select the last .meow element in each row?
The code below just selects all .meows...



$("tr").each(function(){
$(".meow").css("border", "3px solid red");
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr> <td><div class="meow">Meow</div><td>
<td>Woof</td>
<td><div class="meow">Meow</div><td>
</tr>
<tr> <td> <div class="meow">Meow</div><td>
<td><div class="meow">Meow</div></td>
<td>Woof<td>
</tr>
</table>




Answer

Try it like this

$("tr").each(function(){
  $(this).find(".meow:last").css("border", "3px solid red");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr> <td><div class="meow">Meow</div><td>
       <td>Woof</td>
       <td><div class="meow">Meow</div><td>
  </tr>
  <tr> <td> <div class="meow">Meow</div><td>
       <td><div class="meow">Meow</div></td>
       <td>Woof<td>
   </tr>
  </table>