javascript - how to pull href attribute inside the table element -


i have table , each cell of table has specific url. trying url on specific cell using jquery. problem here no class or id defined table, row , column. want pull href attribute using tags only.

         <table>              <tr>                 <td><a href='mytestsite11.com'>site21</a></td>                 <td><a href='mytestsite12.com'>site22</a></td>                 <td><a href='mytestsite13.com'>site23</a></td>             </tr>             <tr>                 <td><a href='mytestsite21.com'>site21</a></td>                 <td><a href='mytestsite22.com'>site22</a></td>                 <td><a href='mytestsite23.com'>site23</a></td>             </tr>              <tr>                 <td><a href='mytestsite31.com'>site21</a></td>                 <td><a href='mytestsite32.com'>site22</a></td>                 <td><a href='mytestsite33.com'>site23</a></td>             </tr>         </table> 

i trying href element of second row second column mytestsite22. have tried following jquery code returning me undefine.not sure missed on this.

        $(function () {             var $row = $('table tr').eq(1);             var $col=$row.eq(1)             alert($col.attr('href'));         }); 

you forgot specify element in second variable:

var $col=$row.find('td').eq(1) 

then, specify anchor inside that:

alert($col.find('a').attr('href')); 

for it's worth, dollar character variable prefix conventionally used indicate array or set. here, you're using on single item.


Comments