My attempt to grab the hrefs from a bunch of & lt; a & gt; tags: Explain Where I Go

advertisements

From a tbody element

<tbody id="results">
   <tr>
      <td><input type="checkbox"><a href="/../../Assets/Microsoft-Azure-roman_column.png">/../../Assets/Microsoft-Azure-roman_column.png</a></td>
      <td>Microsoft</td>
      <td>Azure</td>
      <td>roman_column.png</td>
   </tr>
   <tr>
      <td><input type="checkbox"><a href="/../../Assets/Microsoft-Azure-runphp.cmd">/../../Assets/Microsoft-Azure-runphp.cmd</a></td>
      <td>Microsoft</td>
      <td>Azure</td>
      <td>runphp.cmd</td>
   </tr>
   <tr>
      <td><input type="checkbox"><a href="/../../Assets/Microsoft-Azure-runphp.cmd">/../../Assets/Microsoft-Azure-runphp.cmd</a></td>
      <td>Microsoft</td>
      <td>Azure</td>
      <td>runphp.cmd</td>
   </tr>
   <tr>
      <td><input type="checkbox"><a href="/../../Assets/Microsoft-Azure-Picture1.png">/../../Assets/Microsoft-Azure-Picture1.png</a></td>
      <td>Microsoft</td>
      <td>Azure</td>
      <td>Picture1.png</td>
   </tr>
   <tr>
      <td><input type="checkbox"><a href="/../../Assets/Microsoft-Azure-vertical-align-scrnsht.png">/../../Assets/Microsoft-Azure-vertical-align-scrnsht.png</a></td>
      <td>Microsoft</td>
      <td>Azure</td>
      <td>vertical-align-scrnsht.png</td>
   </tr>
   <tr>
      <td><input type="checkbox"><a href="/../../Assets/Microsoft-Azure-vertical-align-scrnsht.png">/../../Assets/Microsoft-Azure-vertical-align-scrnsht.png</a></td>
      <td>Microsoft</td>
      <td>Azure</td>
      <td>vertical-align-scrnsht.png</td>
   </tr>
</tbody>

my procedure attempts to grab the hrefs and stick them in an array called links:

        var resultRows = $('#results > tr > td > a');
        for (var thisAnchor in resultRows) links.push($(this).attr('href'));
        for (var thisLink in links) console.log(thisLink); // test

But that test is logging

0
1
.
.
.
171
172

to the console rather than the expected

/../../Assets/Microsoft-Azure-roman_column.png
/../../Assets/Microsoft-Azure-runphp.cmd
/../../Assets/Microsoft-Azure-runphp.cmd
/../../Assets/Microsoft-Azure-Picture1.png
/../../Assets/Microsoft-Azure-vertical-align-scrnsht.png
/../../Assets/Microsoft-Azure-vertical-align-scrnsht.png

Why is that and how do I fix it?


You could just use jQuery .each, like this:

$('tr').each(function() {
var href = $(this).find('a').attr('href');
links.push(href);
console.log(href);
});