Example 1 — Official: $("li").odd()
Mark the 2nd and 4th list items (jQuery API demo).
$("li").odd().append(" [odd]"); How It Works
- Select. Start with every
li. - Index. Positions are 0, 1, 2, 3, 4.
- Odd. Keep only indexes 1 and 3.

.odd() (since jQuery 3.5) reduces the matched set to elements at odd zero-based indexes — 1, 3, 5, …. It takes no arguments. Returns a new jQuery object; does not change the DOM.
Start with these common calls. Keep them short and readable.
$("li").odd(); // indexes 1, 3 → items 2 & 4
$("ul li").odd(); // official highlight demo
$("tr").odd(); // zebra table rows (2nd, 4th, …)
// Indexes: 0 1 2 3 4
// Items: 1st 2nd 3rd 4th 5th
// .odd() → ✓ ✓
// .even() → ✓ ✓ ✓ | Call | What it does |
|---|---|
Matched set | Collection to reduce |
Arguments | Method takes no parameters |
Index base | Zero-based position in the set |
Return value | New jQuery set (0 or more) |
Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.
| Parameter | Description |
|---|---|
| Step 1 | Confirm you are on jQuery 3.5+. |
| Step 2 | Select the collection (e.g. $("ul li")). |
| Step 3 | Call .odd() — no arguments. |
| Return Value | Description |
|---|---|
| jQuery object | Most setters and traversing helpers return the jQuery collection so you can chain more methods. |
| Value / undefined | Getters usually return a value from the first matched element, or undefined when nothing applies. |
$("li").odd()Mark the 2nd and 4th list items (jQuery API demo).
$("li").odd().append(" [odd]"); li.$("ul li").odd()Highlight odd-indexed items inside a list (jQuery API example).
$("ul li").odd().append(" [highlight]"); $("ul li") limits the set to list items in the ul..odd() keeps odd indexes only.li is index 0 (even) — skipped..odd() vs .even()Split one list into two non-overlapping groups.
$("#demo li").odd().append(" [odd]");
$("#demo li").even().append(" [even]"); Get the closest positioned ancestor for offset calculations.
CODETOFUN NEWS
Get the latest IT news, programming updates, and technology trends directly on your phone.
Your daily dose of tech, straight to your feed.
10 people found this page helpful