jQuery :odd Selector

The jQuery :odd selector selects the odd items from the group element. The group element can be a table, list, or any other HTML element. After selecting the items, you can apply the CSS using jQuery.

The syntax of the selector is given below.

Syntax
$(“:odd”)

You can use the selector with the HTML tag name. To do this, you have to place the tag name or class name element before the selector. For example: if you have to select the table in a webpage, you have to use the selector as $("table:odd").

Let’s make it more clear with the examples given below.

jQuery :odd Selector to Select Odd Rows of a Table

The rows of a table element start with an index 0. The jQuery :odd selector in the example below selects the odd rows present at 1, 3, 5, and so on. We are using the button in the example below to apply the CSS on click using jQuery.

See the example below to learn the way of using the selector in jQuery.

Example

Test it Live

Output

Sr No Tennis Player Name Score
1 Roger Federer 19
2 Andy Murray 12
3 Serena Williams 23
4 Nick Kyrgois 14
5 Darla Kasatkina 11

The above example contains the table and the button element. There are six rows in the above table including the row containing the heading.

Click the button in the above example to apply the CSS to the odd rows of a table. It applies the red color to the rows having index 1, 3.

Select Odd Items From List Element

Let’s use the list element and select the odd items with jQuery :odd selector. It applies the CSS color to the selected list items when you click the button given below. The items of the list element start from an index 0. If you want to select the odd items, you have to select the list items located at index 1, 3, 5, and so on.

See the example below to perform this task.

Example

Test it Live

Output


  • Learn from simple live examples.
  • Create a jQuery effect easily after learning from Tutorialdeep.
  • Color odd items of a list using this example.
  • Click the button to apply a color to the items
  • Select required items from list element in a webpage using jQuery.

The above example contains the five list items and the button. If you want to apply the CSS color to the odd item then click the button given above. The selected list items are at index 1 and 3. The above example applies the ‘orange’ color only to these selected list items.