This is the case: I have multiple <span>
tags with the class .productLink
, with a link (<a>
) containing a link to a page.
Example
<span class="productLink">
<a href="/Products/Category/item.html">A very nice product</a>
</span>
<span class="productLink">
<a href="/Products/Category/otheritem.html">Also very nice product</a>
</span>
Now I would like to retrieve these links with jQuery.
I tried:
$('.productLink').each(function(index){
console.log($(this + ' a').attr('href'));
});
But it threw:
Uncaught Syntax error, unrecognized expression: [object HTMLSpanElement]
What do I have to change?
You have to select the anchor tag inside jquery selector like this:
$('.productLink > a').each(function(index){
console.log($(this ).attr('href'));
});
Try it here