Squirrl Squirrl - 5 months ago 10
jQuery Question

How to get an href within a HTML element?

I'm looping through a list of objects like this:

for (var i=0;i<rows.length;i++) {
console.log(rows[i].children[2])
}


and it's outputting html like this:

<td style="padding-left: 1em">
<a href="/assets/images/royce/royce-trumbull-09.jpg">royce-trumbull-09.jpg</a>
</td>


And I'm simply trying to get the href attribute from each
<td>
like the one above. I tried
rows[i].children[2].getAttribute("href")
and
rows[i].children[2].href
among other things. I just want to return
"/assets/images/royce/royce-trumbull-09.jpg"

Answer

Looks like you need

rows[i].children[2].children[0].getAttribute("href")

right? Since your output for rows[i].children[2] is the td element, not the a inside it.