<html>
<head>
<script type="text/javascript" src="jquery-1.7.1.js" ></script>
<script type="text/javascript">
$(function() {
$("p:odd").html('pawned');
});
</script>
</head>
<body>
<p>1</p>
<p>2</p>
<p>3</p>
<p>4</p>
</body>
</html>
Output –
1
pawned
3
pawned
Because it utilizes 0-based indexing.
1is the 0 index (which is even) and so forth.Reference
Arrays are 0-based in JS, and jQuery objects wrap elements in an array-like structure. Naturally, most jQuery methods utilize 0-based indexing unless explicitly stated otherwise – as
nth-child(), which is 1-indexed due to being strictly derived from the CSS spec.