Shahmeer Navid
Shahmeer Navid

Reputation: 566

Target sub element of This Object in jQuery?

How can I get a sub element from $(this)?

So for example, how would I target a span element within the this object?

Upvotes: 2

Views: 220

Answers (1)

James Allardice
James Allardice

Reputation: 166031

You could use the find method:

$(this).find("span");

That will find all span elements that are descendants of the element referred to by this.

If you only care about direct children you could use children instead:

$(this).children("span");

Alternatively, you could use this as the context to a selector:

$("span", this);

Yet another solution would be required if this was a jQuery object that contained a set of sibling elements (so the span is not a descendant). In that case, you would need filter:

$(this).filter("span");

Upvotes: 5

Related Questions