Skip to content

Instantly share code, notes, and snippets.

@fabioyamate
Created October 16, 2014 19:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fabioyamate/2c5bb93626c5713552fc to your computer and use it in GitHub Desktop.
Save fabioyamate/2c5bb93626c5713552fc to your computer and use it in GitHub Desktop.
nth-last-child
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<style>
li:nth-last-child(3) { background-color: red; }
</style>
</head>
<body>
<div id="container">
<button>add</button>
<ul>
<li>Foo</li>
<li>Foo</li>
</ul>
<div id="log"></div>
</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<script>
$(function($) {
$('button').on('click', function() {
$('ul').append('<li>' + new Date() + '</li>');
$('#log').text($('li:nth-last-child(3)').text());
});
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment