I am having a difficult time wrapping my head around the jQuery eq. Can someone explain its use to me? What and how does it index?
thanks.
eq() returns it as a jQuery object, meaning the DOM element is wrapped in the jQuery wrapper, which means that it accepts jQuery functions. . get() returns an array of raw DOM elements. You may manipulate each of them by accessing its attributes and invoking its functions as you would on a raw DOM element.
The jQuery syntax is tailor-made for selecting HTML elements and performing some action on the element(s). Basic syntax is: $(selector).action() A $ sign to define/access jQuery. A (selector) to "query (or find)" HTML elements. A jQuery action() to be performed on the element(s)
:eq() is letting you know which element in row we are looking for (this is starting from 0). If you have a list with many “li” elements, then the first will be referred to as li:eq(0). So the CSS selector: html > body > div:eq(0) > ol > li > h3 > a:eq(1)
In the first formulation listed above, jQuery() — which can also be written as $() — searches through the DOM for any elements that match the provided selector and creates a new jQuery object that references these elements: 1. $( "div.
With this HTML:
<ul>
<li>Mario</li>
<li>Luigi</li>
<li>Princess</li>
<li>Toad</li>
</ul>
And this JavaScript:
alert($("ul li").eq(0).text()); // Mario
alert($("ul li").eq(1).text()); // Luigi
alert($("ul li").eq(2).text()); // Princess
alert($("ul li").eq(3).text()); // Toad
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With