I wonder what the best way to make an entire tr clickable would be?
The most common (and only?) solution seems to be using JavaScript, by using onclick="javascript:document.location.href('bla.htm');" (not to forget: Setting a proper cursor with onmouseover/onmouseout).
While that works, it is a pity that the target URL is not visible in the status bar of a browser, unlike normal links.
So I just wonder if there is any room for optimization? Is it possible to display the URL that will be navigated to in the status bar of the browser? Or is there even a non-JavaScript way to make a tr clickable?
If you don't want to use javascript, you can do what Chris Porter suggested by wrapping each td element's content in matching anchor tags. Then set the anchor tags to
display: block
and set theheight
andline-height
to be the same as the td's height. You should then find that the td's touch seamlessly and the effect is that the whole row is clickable. Watch out for padding on the td, which will cause gaps in the clickable area. Instead, apply padding to the anchor tags as it will form part of the clickable area if you do that.I also like to set the row up to have a highlight effect by applying a different background color on tr:hover.
Example
For the latest Bootstrap (version 3.0.2), here's some quick CSS to show how this can be done:
Here's a sample table to work with:
Here's an example showing this in action.
I have found this solution which works quite well:
Just don't forget to style the cursor as a pointer on
tr:hover
Source: http://www.electrictoolbox.com/jquey-make-entire-table-row-clickable/
With jQuery you can do something along these lines:
Then add a highlight_row to your CSS file and that row will change its class to highlight_row. You could swap out whatever you want to do in that line (as well as change $('tr') to fit your specific row.
"
The most common (and only?) solution seems to be using JavaScript, by using
onclick="javascript:document.location.href('bla.htm');"
(not to forget: Setting a proper cursor with onmouseover/onmouseout)."
The onclick-command should look like this:
And it isn't necessary to do anything onmouseover/-out about the cursor as a cursor-property only works when the mouse is hovering the element:
If your table does not have links inside, following trick should work.
Put entire table into a link and change the href attribute of the link in rows onmouseover events.
Demo code:
If your targeted browsers all support CSS Table display styles, you can use Javascript to wrap each row in an
<a>
tag styled to function as a<tbody>
.Here's some JS code using jQuery to make it happen: (jsfiddle)
This code will transform a table that looks like this:
Into this DOM structure in the browser:
Browsers don't seem to be capable of parsing this structure as HTML code (and needless to say it won't validate), it needs to be constructed using JS