Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add hyperlink to table row <tr>

I am having a table with its table row <tr> generating in a loop to form multiple rows.

I want to give separate <a> link to each <tr>. Since in table we can add only add data to <td> only, I am not able to achieve that.

Is there any other way to achieve this?

like image 702
Rajasekar Avatar asked Jan 08 '11 06:01

Rajasekar


People also ask

How do you make a whole row in a table clickable as a link?

To make the entire row as clickable in this case, one can use a link <a> tag to wrap its content.

How do you make a row a link?

To add hyperlinks to an existing Hyperlinks section, right-click a row and click Insert Row on the shortcut menu. You can reference these cells by their row name, which appears in a ShapeSheet window in red text. To assign meaningful names to Hyperlink.


3 Answers

Html:

<table>
    <tr href="http://myspace.com">
      <td>MySpace</td>
    </tr>
    <tr href="http://apple.com">
      <td>Apple</td>
    </tr>
    <tr href="http://google.com">
      <td>Google</td>
    </tr>
</table>

JavaScript using jQuery Library:

$(document).ready(function(){
    $('table tr').click(function(){
        window.location = $(this).attr('href');
        return false;
    });
});

You can try this here: http://jsbin.com/ikada3

CSS (optional):

table tr {
    cursor: pointer;
}

OR the HTML valid version with data-href instead of href:

<table>
    <tr data-href="http://myspace.com">
      <td>MySpace</td>
    </tr>
    <tr data-href="http://apple.com">
      <td>Apple</td>
    </tr>
    <tr data-href="http://google.com">
      <td>Google</td>
    </tr>
</table>

JS:

$(document).ready(function(){
    $('table tr').click(function(){
        window.location = $(this).data('href');
        return false;
    });
});

CSS:

table tr[data-href] {
    cursor: pointer;
}
like image 130
ahmet2106 Avatar answered Oct 12 '22 13:10

ahmet2106


Playing off of @ahmet2016 and keeping it W3C standard.

HTML:

<tr data-href='LINK GOES HERE'>
    <td>HappyDays.com</td>
</tr>

CSS:

*[data-href] {
    cursor: pointer;
}

jQuery:

$(function(){       
    $('*[data-href]').click(function(){
        window.location = $(this).data('href');
        return false;
    });
});
like image 39
Michael J. Calkins Avatar answered Oct 12 '22 12:10

Michael J. Calkins


The easiest way I've found to turn a table row into a link is to use the onclick attribute with window.location.

<table>
<tr onclick="window.location='/just/a/link.html'">
<td></td>
</tr>
</table>
like image 35
jonstout Avatar answered Oct 12 '22 12:10

jonstout