I'm using Bootstrap and the following doesn't work:
<tbody> <a href="#"> <tr> <td>Blah Blah</td> <td>1234567</td> <td>£158,000</td> </tr> </a> </tbody>
Via data attributesrowlink and attribute data-link="row" to a <table> or <tbody> element. For other options append the name to data-, as in data-target="a. mainlink" A cell can be excluded by adding the . rowlink-skip class to the <td> .
To make the full row selectable, you should add the link to the <tr> instead of to individual table cells. Since you're not allowed to place an <a> tag around a table row, you'll have to think a little bit outside the box to solve this puzzle.
You just put an img tag inside the table cell (< td > tag). The src attribute's value can be any valid URL of an image on the Web , local or remote.
With all browsers including IE6 you can attach JavaScript directly to the cell to handle the click. It is only if you really need the <a> tag that you should use one. A lot of people use <a href="#"> when it is easier to just use a tag that is alrerady there or <span> if there isn't a tag to attach the onclick to.
Please look at other answers below, especially ones that do not use jquery.
Preserved for posterity but surely the wrong approach in 2020. (Was non idiomatic even back in 2017)
You are using Bootstrap which means you are using jQuery :^), so one way to do it is:
<tbody> <tr class='clickable-row' data-href='url://'> <td>Blah Blah</td> <td>1234567</td> <td>£158,000</td> </tr> </tbody> jQuery(document).ready(function($) { $(".clickable-row").click(function() { window.location = $(this).data("href"); }); });
Of course you don't have to use href or switch locations, you can do whatever you like in the click handler function. Read up on jQuery
and how to write handlers;
Advantage of using a class over id is that you can apply the solution to multiple rows:
<tbody> <tr class='clickable-row' data-href='url://link-for-first-row/'> <td>Blah Blah</td> <td>1234567</td> <td>£158,000</td> </tr> <tr class='clickable-row' data-href='url://some-other-link/'> <td>More money</td> <td>1234567</td> <td>£800,000</td> </tr> </tbody>
and your code base doesn't change. The same handler would take care of all the rows.
You can use Bootstrap jQuery callbacks like this (in a document.ready
callback):
$("#container").on('click-row.bs.table', function (e, row, $element) { window.location = $element.data('href'); });
This has the advantage of not being reset upon table sorting (which happens with the other option).
Since this was posted window.document.location
is obsolete (or deprecated at the very least) use window.location
instead.
You can't do that. It is invalid HTML. You can't put a <a>
in between a <tbody>
and a <tr>
. Try this instead:
<tr onclick="window.location='#';"> ... </tr>
add style for pointer view
[data-href] { cursor: pointer; }
When you work up to it, you'd want to use JavaScript to assign the click handler outside the HTML.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With