Compare :last vs :last-child vs :last-of-type - Javascript jQuery Selector

Javascript examples for jQuery Selector:last-child

Description

Compare :last vs :last-child vs :last-of-type

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
        var btn = $(this).text();
        $("p").css("background-color", "white");
        $("p" + btn).css("background-color", "yellow");
    });/*from  w ww .  j a v a2  s  .  c  om*/
});
</script>
</head>
<body>

<button>:last</button>
<button>:last-child</button>
<button>:last-of-type</button><br>

<p>A</p>

<div style="border:1px solid;">
  <p>B</p>
  <p>C</p>
</div><br>

<div style="border:1px solid;">
  <span>D</span>
  <p>E</p>
  <p>F</p>
  <span>G</span>
</div><br>

<div style="border:1px solid">
  <p>H</p>
  <p>I</p>
</div>

<p>The last paragraph in body, and the last child in div.</p>

</body>
</html>

Related Tutorials