12

How do I make this entire td a link?

<td id="blue-border"><span id="blue"></span></td>

Clicking td should make it behave like this (I know this is syntactically incorrect:

<a href="javascript:chooseStyle('blue-theme', 360)"> <td id="blue-border"><span id="blue"></span></td> </a>

EDIT: so far all the suggestions are only making the span inside the td a link, help lol.

user784637
  • 15,392
  • 32
  • 93
  • 156
  • What are you trying to do? Why does the table cell have to be linkable? – kdub Jun 23 '11 at 23:00
  • It then acts like a giant button, which could be very mobile-friendly. – Demis Jun 16 '21 at 00:46
  • I concur with the OP - none of the solutions make the whole TD, including the TD's padding/margins, clickable! The `onclick=` method might work, but nobody's yet provided a working example of making it hyperlink. – Demis Jun 16 '21 at 03:03

7 Answers7

28

Use CSS.

td a { display: block; width: 100%; height: 100%; }
<table style="width: 100%">
  <tr>
    <td><a href="#">Link</a></td>
  </tr>
</table>

The CSS forces the link to expand to the full width and height of the TD.

Flimm
  • 136,138
  • 45
  • 251
  • 267
Will Martin
  • 4,142
  • 1
  • 27
  • 38
3

You can't wrap a td in an anchor. Do this:

<td id="blue-border"><a href="javascript:chooseStyle('green-theme', 360)"> <span id="blue"></span></a></td> 

Or

<td onclick="chooseStyle('green-theme', 360)" id="blue-border"><span id="blue"></span></td>
John Kalberer
  • 5,690
  • 1
  • 23
  • 27
  • What would the syntax be for making the hyperlink to a URL? Looks like this example runs some undefined function that presumably alters the color, instead of making a hyperlink. – Demis Jun 16 '21 at 03:00
3

Add an anchor tag inside of the td and set its display attribute to block. This should make the entire td clickable.

#blue-border a{
    display: block;
}

or

<a href="link" style="display:block;">
rolling stone
  • 12,668
  • 9
  • 45
  • 63
2

Define an OnClick event for the td:

<td id="blue-border" onclick="chooseStyle('blue-theme', 360)">...
n8wrl
  • 19,439
  • 4
  • 63
  • 103
2

If all you're doing is firing javascript, I'd suggest using onclick instead of an anchor tag in the first place, like:

<td id="cell123" onclick="chooseStyle('green-theme',360)">cell contents</td>

You can throw a simple css style on there if you want the mouse to become a pointer:

#cell123:hover { cursor: pointer; }
Joe Enos
  • 39,478
  • 11
  • 80
  • 136
  • How does `chooseStyle()` correspond to making the TD act as a clickable hyperlink to a URL? – Demis Jun 16 '21 at 03:02
2
<table width="100%" class="blueCss">
    <tr>
        <td ID="tdBlue">
            <span id="Blue">Hello</span>
        </td>  
        <td>
            <span>other col</span>
        </td>
     </tr>
</table>

css file:

.blueCss {
        height: 100px;
        width: 100px;
    }

    .blueCss td {
        background-color: blue;
    }

    .blueCss:hover    {
        border-color: #00ae00;
    }

    .blueCss td:hover    {
        background-color: yellow;
        cursor: pointer;
    }

jQuery code:


$(document).ready(function(){
    var tdLink = $('#tdBlue');

    tdLink.click(function(){
         alert('blue-theme');
    });
});

Check here: jsFiddle.net

Marco Hurtado
  • 534
  • 3
  • 13
0

Use jquery with class
$("tr td.data_col").click(function() { window.location = $(this).find('a').attr("href"); });