simon
simon

Reputation: 2377

How to select this > a

I always forget how to select an element inside this in jQuery. I tried something like $(this+'a') but unfurtunately that does not work correctly. Here's a share of my code that i'm currently working with:

var currlink = $(this+'a').attr('href');

Good weeekend! :)

Upvotes: 1

Views: 89

Answers (3)

Luke Madhanga
Luke Madhanga

Reputation: 7467

You can also use the find() function:

$(this).find('a').prop('href'); 

For the time being, .prop acts almost similarly to .attr with a few discrepancies, but .attr is deprecated in the lates versions of jQuery

You can also do

$(this).find('[href=http://domain.tld]');

The above will get an element whose href is equal to http://domain.tld

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1039160

var currlink = $('a', this).attr('href');

Upvotes: 3

zmbq
zmbq

Reputation: 39023

If you want to select an element that is contained in another element in the DOM, do this:

var outer = $(this)
var inner = $('a', outer)

Or, in short:

$('a', this).attr('href')

Upvotes: 0

Related Questions