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 :eq() Selector
Photo Credit to CodeToFun
🙋 Introduction
In the realm of web development, jQuery stands out as a powerful library for simplifying JavaScript tasks. Among its arsenal of selectors is the :eq()
selector, which allows you to target specific elements based on their index within a set of matched elements. Understanding and harnessing the potential of the :eq()
selector can significantly enhance your ability to manipulate HTML elements dynamically.
This guide aims to demystify the jQuery :eq()
selector through comprehensive examples and clear explanations.
🧠 Understanding :eq() Selector
The :eq()
selector enables you to select elements based on their index within a set of matched elements. It provides a convenient way to target specific elements in a collection, allowing for precise manipulation and interaction.
💡 Syntax
The syntax for the :eq()
selector is straightforward:
$("selector:eq(index)")
📝 Example
Selecting Elements by Index:
Suppose you have a list of items and you want to select and manipulate the second item. You can achieve this using the
:eq()
selector as follows:index.htmlCopied<ul> <li>Item 1</li> <li>Item 2</li> <li>Item 3</li> </ul>
example.jsCopied$("li:eq(1)").css("color", "red");
This will change the text color of the second <li> element to red.
Applying Styles to Specific Elements:
You can use the
:eq()
selector to apply styles to elements at specific indices within a set. For instance:index.htmlCopied<div class="box">Box 1</div> <div class="box">Box 2</div> <div class="box">Box 3</div>
example.jsCopied$(".box:eq(2)").css("background-color", "blue");
This will set the background color of the third .box element to blue.
Event Handling on Specific Elements:
You can bind events to elements selected by the
:eq()
selector. Here's an example where we alert a message when the second button is clicked:index.htmlCopied<button>Button 1</button> <button>Button 2</button> <button>Button 3</button>
example.jsCopied$("button:eq(1)").click(function() { alert("Button 2 clicked!"); });
Targeting Last or Specific Elements:
The
:eq()
selector allows you to target elements at specific indices within a set. Additionally, you can use negative indices to select elements from the end of the set. For example, :eq(-1) selects the last element, :eq(-2) selects the second-to-last element, and so on.
🎉 Conclusion
The jQuery :eq()
selector provides a powerful mechanism for targeting specific elements within a set based on their index. Whether you need to apply styles, handle events, or manipulate elements dynamically, this selector offers a versatile solution.
By mastering its usage, you can enhance the interactivity and functionality of your web pages with ease.
👨💻 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 :eq() Selector), please comment here. I will help you immediately.