jQuery Basic
jQuery Selectors
- jQuery Selectors
- jQuery *
- jQuery :animated
- jQuery [name|=”value”]
- jQuery [name*=”value”]
- jQuery [name~=”value”]
- jQuery [name$=”value”]
- jQuery [name=”value”]
- jQuery [name!=”value”]
- jQuery [name^=”value”]
- jQuery :button
- jQuery :checkbox
- jQuery :checked
- jQuery Child Selector
- jQuery .class
- jQuery :contains()
- jQuery Descendant Selector
- jQuery :disabled
- jQuery Element
- jQuery :empty
- jQuery :enabled
- jQuery :eq()
- jQuery :even
- jQuery :file
- jQuery :first-child
- jQuery :first-of-type
- jQuery :first
- jQuery :focus
- jQuery :gt()
- jQuery Has Attribute
- jQuery :has()
- jQuery :header
- jQuery :hidden
- jQuery #id
- jQuery :image
- jQuery :input
- jQuery :lang()
- jQuery :last-child
- jQuery :last-of-type
- jQuery :last
- jQuery :lt()
- jQuery [name=”value”][name2=”value2″]
- jQuery (“selector1, selector2, selectorN”)
- jQuery (“prev + next”)
- jQuery (“prev ~ siblings”)
- jQuery :not()
- jQuery :nth-child()
- jQuery :nth-last-child()
- jQuery :nth-last-of-type()
- jQuery :nth-of-type()
- jQuery :odd
- jQuery :only-child
- jQuery :only-of-type
- jQuery :parent
- jQuery :password
- jQuery :radio
- jQuery :reset
- jQuery :root
- jQuery :selected
- jQuery :submit
- jQuery :target
- jQuery :text
- jQuery :visible
jQuery :odd Selector
Photo Credit to CodeToFun
🙋 Introduction
In the realm of jQuery, selectors play a crucial role in targeting specific elements within a document. One such selector is :odd
, which allows you to select elements with odd indices within a matched set. Understanding and leveraging the :odd
selector can streamline your development process and enhance the interactivity of your web pages.
This guide aims to explore the functionality of the jQuery :odd
selector through comprehensive examples and explanations.
🧠 Understanding :odd Selector
The :odd
selector targets elements with odd indices within a set of matched elements. It is particularly useful when you want to style or manipulate every other element in a collection, such as alternating rows in a table or a series of list items.
💡 Syntax
The syntax for the :odd
selector is straightforward:
$(":odd")
📝 Example
Styling Odd Rows in a Table:
Consider a table where you want to apply a different background color to every other row. You can achieve this effortlessly using the
:odd
selector:index.htmlCopied<table> <tr><td>Row 1</td></tr> <tr><td>Row 2</td></tr> <tr><td>Row 3</td></tr> <tr><td>Row 4</td></tr> <tr><td>Row 5</td></tr> </table>
example.jsCopied$("table tr:odd").css("background-color", "lightgray");
This will set the background color of odd rows in the table to light gray.
Selecting Odd List Items:
Suppose you have an unordered list and you want to select and manipulate only the odd-numbered list items:
index.htmlCopied<ul> <li>Item 1</li> <li>Item 2</li> <li>Item 3</li> <li>Item 4</li> <li>Item 5</li> </ul>
example.jsCopied$("ul li:odd").css("font-weight", "bold");
This will make the text of odd-numbered list items bold.
Alternating Styles for Elements:
You can apply alternating styles to any set of elements using the
:odd
selector. For instance, let's change the text color of odd paragraphs:index.htmlCopied<p>Paragraph 1</p> <p>Paragraph 2</p> <p>Paragraph 3</p> <p>Paragraph 4</p> <p>Paragraph 5</p>
example.jsCopied$("p:odd").css("color", "blue");
This will set the text color of odd paragraphs to blue.
Combining :odd Selector with Other Selectors:
You can combine the
:odd
selector with other selectors to narrow down your selection. For example, to target odd paragraphs within a specific class:example.jsCopied$(".special-class p:odd").css("font-style", "italic");
This will make the text of odd paragraphs within elements with the class special-class italic.
🎉 Conclusion
The jQuery :odd
selector provides a convenient way to target and manipulate elements with odd indices within a set of matched elements. Whether you need to style alternating rows in a table, select specific list items, or apply alternating styles to any set of elements, this selector offers a straightforward solution.
By mastering its usage, you can enhance the visual appeal and interactivity of your web pages effortlessly.
👨💻 Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (jQuery :odd Selector), please comment here. I will help you immediately.