jQuery Selector How to - nth-last-child(2); select second last element








Question

We would like to know how to nth-last-child(2); select second last element.

Answer


<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
div:nth-last-child(2) {<!--from  w  w w.j  a  v  a2s  . co m-->
  color: red;
}
</style>
</head>
<body>
  <div class="my_div">a</div>
  <div class="my_div">b</div>
  <div class="my_div">c</div>
  <div>other</div>
</body>
</html>

The code above is rendered as follows: