2

Inside an $.each() loop, there is one specific case that I don't understand why is failing.

When I do an if statement that says something like this (my specific example, only without the real attribute names)

if(($(this).attr("some-attribute")))

it always returns false, whether or not the attribute exists.

But if i first get the id of this element and then check for the attribute without the "this" keyword, it works okay.

var id = $(this).attr("id");
if(($("#"+id).attr("some-attribute")))

This works fine and it does return the correct value.

3Nex
  • 517
  • 6
  • 14
  • Could you give some context as to when/how to reproduce this issue, such as in a http://jsfiddle.net? – Fabrício Matté Oct 16 '12 at 01:23
  • I don't mean to be pedantic, and it's probably filler text, but if your attribute has a space in it that's not a valid attribute. Can you post the actual code, or edit [this fiddle](http://jsfiddle.net/jimschubert/n69jt/)? – Joseph Yaduvanshi Oct 16 '12 at 01:35

1 Answers1

6

$(this) Doesn't query the DOM, it just wraps the javascript DOM element with a jQuery object.

$('#id') Does query the DOM.

I suggest you reading my answer here

Community
  • 1
  • 1
gdoron
  • 147,333
  • 58
  • 291
  • 367
  • Even though this answers the question title, the actual problem seems a bit different. – Fabrício Matté Oct 16 '12 at 01:21
  • @FabrícioMatté. The real code is missing, he's probably having a typo error, as the scenario he describes is **impossible** as I see it. – gdoron Oct 16 '12 at 01:23
  • @gdoron That's exactly I meant. `=]` – Fabrício Matté Oct 16 '12 at 01:27
  • Well, i have no idea what to tell you guys. I am unable to reproduce the issue anymore, it seems to work both ways now. Maybe indeed it was a typo or just a clash with other parts of code that i didn't notice... Sorry to waste everyone's time – 3Nex Oct 16 '12 at 14:18