3Nex
3Nex

Reputation: 527

What's the difference between $(this) and $("#id")?

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.

Upvotes: 2

Views: 221

Answers (1)

gdoron
gdoron

Reputation: 150253

$(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

Upvotes: 6

Related Questions