How does jquery determines target

If we use Jquery to target specific even or odd elements like following code snippet
$(".target:even").addClass("animated shake");
How does Jquery execute this statement, is there any way to predict which elements are targeted.

The jQuery documentation for the :even selector states that since indexing for this selector is zero-based it’s counter intuitive that the first, third, fifth, and so on elements in the match will be selected.

Was this what you were asking or did I misinterpret?

The HTML element selected (or “found”) is inside $(" "). What follows the “.” (in this case, “addClass”) is an action to be performed on the element. What follows, inside the (" "), is, in this case, the class of “animated shake.”

In 0-indexing, your example is selecting all even elements (first, third, etc.) in the HTML class called “target.” jQuery documentation

1 Like

I am asking about working of .target not the selector. I want to know how is ‘target’ replaced by relevant DOM element.

Thanks for your reply madame but I want to know how target works how does it create parse tree for parsing DOM elements.

In this case .target matches to a CSS element with ...class="target".... The dot prior to target identifies it as a class attribute. In the example you provided the first, third, fifth, etc. elements having ...class="target"... will have “animated” and “shake” added to their class attributes. I hope this helps.

1 Like

You would have to have at least one HTML element with the class “target” in order for that code snippet to do anything at all.

1 Like

Another question to the same .odd/.even jQuery topic.
Challenge 112 requests “Try selecting all the even-numbered elements”. Stricly speeking this should require the .odd selector doesn’t it (being 0-based)?
However, the challange is accepted with the .even selector, which I feel selects the odd elements.
Does anyone agree with this line of thought?

In the statement “:odd selects the second element, fourth element, and so on,” the second element is index 1, which is odd; the fourth element is index 3, which is also odd.

To further clarify: in zero-based numbering, the initial element of a sequence is assigned the index “0,” not “1,” as it is in everyday/non-programming circumstances.

i went through this too but i think its just the way the question is structured.

when they refer to even they arent referring the VALUES of the elements but rather the INDEX number of the elements