Sopan Maiti
Sopan Maiti

Reputation: 189

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>

Upvotes: 0

Views: 119

Answers (4)

Edi G.
Edi G.

Reputation: 2422

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

Upvotes: 1

Ravi Jiyani
Ravi Jiyani

Reputation: 959

You can use $("table tr th a").text(); or $("table tr th a").html();

Upvotes: 0

V31
V31

Reputation: 7666

Using Jquery you can use the selector

$('table tr th a').html()

Working Fiddle

Upvotes: 1

allnodcoms
allnodcoms

Reputation: 1262

I would give your link tag and ID, then use:

document.getElementByID(linkID).href;

Upvotes: 0

Related Questions