Skip to content

Instantly share code, notes, and snippets.

@willhalling
Created August 6, 2013 09:53
Show Gist options
  • Save willhalling/6163249 to your computer and use it in GitHub Desktop.
Save willhalling/6163249 to your computer and use it in GitHub Desktop.
Identify first item.
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<title>Identify first item</title>
<script type="text/javascript">
$(function() {
// add .first class to identify first div
$('#wrap').children().each(function(i,obj){
$this = $(this); // cache $(this)
if (i <= 0) {
$this.children().addClass('first');
$this.css('background','red');
}
});
});
</script>
</head>
<body>
<div id="wrap">
<div id="div-1">div 1</div>
<div id="div-2">div 2</div>
<div id="div-3">div 3</div>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment