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
  • 507
  • 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/)? – Jim Schubert 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 is supporting Monica
  • 142,542
  • 55
  • 282
  • 355