Skip to main content

Bug Tracker

Side navigation

#13163 closed bug (notabug)

Opened January 06, 2013 10:13PM UTC

Closed January 06, 2013 10:18PM UTC

Last modified January 07, 2013 11:32AM UTC

Can't get .each to work with anchor elements

Reported by: Owned by:
Priority: undecided Milestone: None
Component: unfiled Version: 1.8.3
Keywords: Cc:
Blocked by: Blocking:

I'm going crazy trying to figure out what I'm doing wrong. If I use $('elem_type').each on almost anything, I get the element in this. But if I use it on anchor elements (i.e. <a>), I get the href!

It's probably me, but I've been doing web searches for the last hour and can't find anything telling me why it behaves differently and how to work around it.

I did create a fairly minimal test case at In a nutshell, I expect both the first and second line of JavaScript code to pop up alert boxes with [object HTMLSomeElement]. I get HTMLDivElement when I use div, I get HTMLParagraphElement when I use p, etc. But when I use a, I get the href as a String, not the element. I don't need the href - I can always get that from the property! I need the actual HTMLAnchorElement objects, but I can't seem to get those.

Other methods, like .addClass, seem to work. But in my case I don't see an easy way to get what I need to do done without .each.

Attachments (0)
Change History (2)

Changed January 06, 2013 10:18PM UTC by dmethvin comment:1

resolution: → notabug
status: newclosed

This isn't a bug report, please ask for help on a forum.

Changed January 07, 2013 11:32AM UTC by comment:2

Please accept my sincere apologies. You are completely right and I was wrong. From time to time I do actually find bugs, at least in other projects, and this unfortunately allowed me to think I might have actually found one here. If I could unreport this, I would. In lieu of that, hopefully the following might help someone in a similar situation and thus avoid a repeat ticket at some point.


For anyone else who comes this way, you have not found a bug! You are being mislead by a combination of the following:

  • Don't use alert for debugging, but also don't assume that console.log is transparent. Even if every browser you test has the same behavior, it could just be a DOM implementation quirk!
  • If you are having the same underlying issue that lead me into this trap while attempting to debug, consider the following. JQuery produces wrappers for collections, but the individual items in the collection (either accessed via each or indexing) are not in turn wrapped.