What is the best way to make a tables row a link? I currently am using jquery to zebra stripe the rows and also to highlight the onmouseover/off selected row so if js is the answer please use jquery.
+1
A:
Register a onclick event handler for the tr element. Something like this using jQuery:
$("tr").bind("click", function(){
window.location = 'http://www.example.com/';
});
Ronny Vindenes
2009-02-20 12:26:00
Doesn't this link you to the same location on every row click?
Zaagmans
2009-02-20 12:27:18
You may use a hidden input in each tr to store the url.
Adones Cunha
2009-02-20 12:33:01
This will indeed bind the same link to each row, modify according to your needs..
Ronny Vindenes
2009-02-20 12:38:45
+5
A:
$(document).ready(function(){
$("tr").click(function(){
/* personally I would throw a url attribute (<tr url="http://www.hunterconcepts.com">) on the tr and pull it off on click */
window.location = $(this).attr("url");
});
});
hunter
2009-02-20 15:18:55
+12
A:
I just use css:
<style>
table.collection {width:500px;border-collapse:collapse;}
table.collection tr {background-color:#fff; border-bottom: 1px #99b solid;}
table.collection tr:hover {background-color:#ffe;}
table.collection td {display:table-cell;border-bottom: 1px #99b solid; padding:0px;}
table.collection td a {text-decoration:none; display:block; padding:0px; height:100%;}
</style>
<table class="collection">
<tr>
<td><a href="#">Linky1</a></td>
<td><a href="#">Data1</a></td>
</tr>
<tr>
<td><a href="#">Linky2</a></td>
<td><a href="#">Data2</a></td>
</tr>
</table>
Nick
2009-02-20 15:33:13
Best solution by far. Browser still see's this as a link so 'open in tab' or 'copy link' will work. The other solutions break my browser! There is scope for a JQuery solution that automates the above by DOM manipulation.
andybak
2009-03-25 17:48:59
I'm having trouble with this solution. If one of the table cells contains enough content to wrap lines, the height of neighbouring cells won't stretch to the full height. Have you found a workaround for this case?
troelskn
2009-05-28 09:32:25
This sounds like an IE quirks mode issue. I don't see this in firefox or IE 7-8 with "-//W3C//DTD XHTML 1.0 Strict//EN". It also may not work in IE 6 (but what does?)
Nick
2009-06-11 19:51:12
on second thought - adding height:100%; to the a style would most likely fix it (see the last edit).
Nick
2009-06-11 19:55:16