Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

jQuery Basic

jQuery Ajax Events

jQuery Ajax Methods

jQuery Keyboard Events

jQuery Keyboard Methods

jQuery Form Events

jQuery Form Methods

jQuery Mouse Event

jQuery Mouse Methods

jQuery Event Object

jQuery Fading

jQuery Document Loading

jQuery Traversing

jQuery Utilities

jQuery Property

jQuery HTML

jQuery CSS

jQuery Miscellaneous

jQuery .even() Method

Posted in jQuery Tutorial
Updated on Oct 13, 2024
By Mari Selvan
👁️ 27 - Views
⏳ 4 mins
💬 1 Comment
jQuery .even() Method

Photo Credit to CodeToFun

🙋 Introduction

jQuery offers a plethora of methods to simplify DOM manipulation and traversal, empowering developers to create dynamic and interactive web experiences. Among these methods is .even(), which facilitates the selection of even-indexed elements from a collection. Understanding and utilizing this method can streamline your code and enhance your web development workflow.

In this guide, we'll explore the jQuery .even() method with examples to illustrate its usage and potential benefits.

🧠 Understanding .even() Method

The .even() method in jQuery enables you to filter elements based on their index within a collection. It selects elements at even positions (0-indexed) within the matched set, making it convenient for tasks such as alternating row colors in tables or targeting specific elements in a list.

💡 Syntax

The syntax for the .even() method is straightforward:

syntax.js
Copied
Copy To Clipboard
$(selector).even()

📝 Example

  1. Alternating Row Colors in a Table:

    Suppose you have a table and you want to apply different background colors to alternating rows. You can achieve this effortlessly using the .even() method:

    index.html
    Copied
    Copy To Clipboard
    <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.js
    Copied
    Copy To Clipboard
    $("tr").even().css("background-color", "lightgray");

    This will set the background color of even-indexed rows to light gray, creating a visually appealing alternating pattern.

  2. Selecting Even-numbered List Items:

    If you have an unordered list and you want to target specific items, such as even-numbered ones, you can use the .even() method:

    index.html
    Copied
    Copy To Clipboard
    <ul>
      <li>Item 1</li>
      <li>Item 2</li>
      <li>Item 3</li>
      <li>Item 4</li>
      <li>Item 5</li>
    </ul>
    example.js
    Copied
    Copy To Clipboard
    $("ul li").even().addClass("highlight");

    This will add a CSS class named highlight to even-numbered list items, allowing you to style them differently.

  3. Applying Styles to Even-indexed Elements:

    You can also apply CSS styles directly to even-indexed elements using the .even() method. For example:

    index.html
    Copied
    Copy To Clipboard
    <div class="box">Box 1</div>
    <div class="box">Box 2</div>
    <div class="box">Box 3</div>
    <div class="box">Box 4</div>
    <div class="box">Box 5</div>
    example.js
    Copied
    Copy To Clipboard
    $(".box").even().css("border", "2px solid blue");

    This will add a blue border to elements with even indices within the .box class.

🎉 Conclusion

The jQuery .even() method provides a convenient way to target elements at even positions within a collection, simplifying tasks such as styling, selection, and manipulation. By incorporating this method into your jQuery toolkit, you can streamline your code and enhance the interactivity of your web pages with ease.

Experiment with the .even() method in your projects to discover its versatility and unlock new possibilities for efficient DOM traversal and manipulation.

👨‍💻 Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

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

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy