Pregunta

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! :)

¿Fue útil?

Solución

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

Otros consejos

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')

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

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top