Sopan Maiti Sopan Maiti - 10 days ago 8
Javascript Question

How to retrieve table header data within <a> tag using Javascript or Jquery?

How can I retrieve table header data within

<a>
tag such as
Name
using jQuery or JavaScript?

My html is:

<table style="width:100%">
<tr>
<th><a href="www.a.com">Name<a></th>
<th colspan="2">Telephone</th>
</tr>
<tr>
<td>Bill Gates</td>
<td>555 77 854</td>
<td>555 77 855</td>
</tr>
</table>

Answer

I'm not sure whether I get your question right, but shouldn't the following work?

$('th').find('a').each(function(){console.log($(this).text())});

-> Result in your case Name

<table style="width:100%">
    <tr>
        <th><a href="www.a.com">Name<a></th>
        <th><a href="www.a1.com">Name1<a></th>
        <th><a href="www.a2.com">Name2<a></th>
        <th><a href="www.a3.com">Name3<a></th>
        <th colspan="2">Telephone</th>
    </tr>
    <tr>
        <td>Bill Gates</td>
        <td>555 77 854</td>
        <td>555 77 855</td>
    </tr>
</table>

-> Result is:

Name
Name1
Name2
Name3