boz
boz

Reputation: 4908

How can this jquery selector be shortened?

I've got the following selector:

$(this).children('li').children('a')

It seems a bit clunky, can it be shortened?

Upvotes: 1

Views: 102

Answers (5)

gustavotkg
gustavotkg

Reputation: 4399

The shortest one, in my opinion is $(">li>a",this).

If you are sure there is no other li's in A innerHTML, maybe you can short to $("li>a",this).

Upvotes: 0

erimerturk
erimerturk

Reputation: 4288

try this

$(this).children("li > a")

Upvotes: 0

Jan Dragsbaek
Jan Dragsbaek

Reputation: 8101

It can be shortened very little, so it looks like this:

$('li', this).children('a')

Upvotes: 1

Royi Namir
Royi Namir

Reputation: 148524

$('li > a',this);......................

Upvotes: 4

James Allardice
James Allardice

Reputation: 165971

You could use find, but I don't think it's much shorter really:

$(this).find("> li > a");

Note the use of the > child selector, so it only finds li elements that are direct children of this (as the children method in your original code would).

Upvotes: 2

Related Questions