Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# Demo for different methods of selecting the first child element
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<title>Demo For first child item</title>
<style>
.red{color:red;font-size:20px;}
</style>
</head>
<body>
<h1>Demo for different methods of selecting the first child element</h1>
<ul class="myClass">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
<script>
// uncomment desired one to test:
//$('.myClass > li').eq(0).addClass('red');
//$('.myClass > li:eq(0)').addClass('red');
//$('.myClass > li').first().addClass('red');
//$('.myClass > li:first').addClass('red');
//$('.myClass > li:first-child').addClass('red');
//$('.myClass > li:lt(1)').addClass('red');
//$('.myClass > li').slice(0,1).addClass('red');
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment