Skip to content

Instantly share code, notes, and snippets.

@Abhinav1217
Last active December 14, 2015 10:28
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 Abhinav1217/5071770 to your computer and use it in GitHub Desktop.
Save Abhinav1217/5071770 to your computer and use it in GitHub Desktop.
# 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