0

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>
3
  • I am not able to see your JavaScript code. Commented Feb 12, 2015 at 10:00
  • document.querySelector('a').text will do so.... Commented Feb 12, 2015 at 10:02
  • Thank you Rory for editing. I was not sure how to write html block. And also thanks to all for your answering. Now I am getting lots of way to retrieve. Commented Feb 12, 2015 at 10:15

4 Answers 4

1

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
Sign up to request clarification or add additional context in comments.

Comments

1

Using Jquery you can use the selector

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

Working Fiddle

Comments

0

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

document.getElementByID(linkID).href;

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.