[jQuery] identifying an element with no "id" attribute
I've been struggling with how to identify an element which does not
have an "id" attribute.
here's an example: a page has, say, 5 links on it. of course, I can
get a handle on an array of jquery object just by doing:
$('a')
and I can treat that as an array and loop over it, and can address
particular elements easily, such as:
$('a')[3].text()
which will tell me the words being hyperlinked of the 4th anchor tag.
But if the links don't also have "id" attributes, is there a way to
tell WHICH element was clicked on? I can write a function that does
something onClick of course, and use "this" to reference the element
which had been clicked. But is there a way to say "yes, the 5th
anchor link was the one that got clicked"?
And I don't just mean, "report this info right when it gets clicked" ,
I'm more interested in "how to save this info in such a way that
sometime in the future i 'd be able to say that the 5th anchor link on
this page had been clicked", just like I'd be able to get a handle
onit if it had an "id", such as $('#id') ?
One would think that even in the absence of an "id" attribute, that
there's some internal DOM representation of a unique identifier for
all tags by which the DOM can answer this question
One idea I had was to save, onClick, the values for this.href() and
this.text(), and then loop through all the anchor links and when a
match is found, then that index is the i-th element in that array.
But this work-around breaks down if there are more than one such match
in a page.
also a final question: if a particular link is 5th in the page when
it's loaded, and I move things around visually on the page using
jQuery, does that change the order of the anchor link on the page?