How Do I Make a Full Table Row Clickable?
Update 7/28/2010: I just wrote a new version of this article using jQuery. You can find the new article here.
It's a common scenario to display an HTML table with items, like products, employees and so on, and then link each item to a detail page where you can display the full details of this item.
The link is often represented by a button or text link in one of the columns. But to make it easier for your users to select a specific item, you could make the full row clickable. With a bit of JavaScript, this is pretty easy to do.
Often when people try to implement this behavior, they try to add an HTML link to each table cell, like this:
<table> <tr> <td><a href="Details.asp">John</a></td> <td><a href="Details.asp">Doe</a></td> </tr> </table>
The disadvantage of this method is that not the entire table cell is linked, but just the text in that cell. You also need to add a lot of extra code to make each cell clickable.
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. Instead of using the <a> tag, you can implement a click handler with a JavaScript method attached to it for the table row as in the example below. To clearly communicate to the user that the entire table is clickable, I also added some code that changes the background color of the table row when the mouse hovers over it. This makes it easier to see which row is selected.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Full Row Select</title> <script type="text/javascript"> function ChangeColor(tableRow, highLight) { if (highLight) { tableRow.style.backgroundColor = '#dcfac9'; } else { tableRow.style.backgroundColor = 'white'; } } function DoNav(theUrl) { document.location.href = theUrl; } </script> </head> <body> <table width="100%" border="1" cellpadding="0" cellspacing="0"> <tr onmouseover="ChangeColor(this, true);" onmouseout="ChangeColor(this, false);" onclick="DoNav('http://www.yahoo.com/');"> <td>1</td> <td>2</td> <td>3</td> </tr> <tr onmouseover="ChangeColor(this, true);" onmouseout="ChangeColor(this, false);" onclick="DoNav('http://www.microsoft.com/');"> <td>4</td> <td>5</td> <td>6</td> </tr> <tr onmouseover="ChangeColor(this, true);" onmouseout="ChangeColor(this, false);" onclick="DoNav('http://Imar.Spaanjaars.Com/');"> <td>7</td> <td>8</td> <td>9</td> </tr> </table> </body> </html>
On each <tr> element I have added the onclick attribute that calls the DoNav method. This will take the user to a new page whenever the user has clicked somewhere in the table row. To change the color of the rows when the mouse hovers over the table, I have also added the onmouseover and onmouseout attributes that call the ChangeColor method to change the background color of the table row.
You can see the full sample code here. In the example I also added a bit of CSS to influence the borders of the table, so each row has a nice thin black border at the top and bottom side, but not between the columns on the left and right side. Look at the source of the page in the browser to see how it is done.
Where to Next?
Wonder where to go next? You can post a comment on this article.
Links in this Document
Doc ID | 312 |
Full URL | https://imar.spaanjaars.com/312/how-do-i-make-a-full-table-row-clickable |
Short cut | https://imar.spaanjaars.com/312/ |
Written by | Imar Spaanjaars |
Date Posted | 07/11/2004 18:45 |
Date Last Reviewed | 05/25/2006 21:14 |
Listened to when writing | The State We're In by The Chemical Brothers (Track 7 from the album: Come With Us) |
Comments
Talk Back! Comment on Imar.Spaanjaars.Com
I am interested in what you have to say about this article. Feel free to post any comments, remarks or questions you may have about this article. The Talk Back feature is not meant for technical questions that are not directly related to this article. So, a post like "Hey, can you tell me how I can upload files to a MySQL database in PHP?" is likely to be removed. Also spam and unrealistic job offers will be deleted immediately.
When you post a comment, you have to provide your name and the comment. Your e-mail address is optional and you only need to provide it if you want me to contact you. It will not be displayed along with your comment. I got sick and tired of the comment spam I was receiving, so I have protected this page with a simple calculation exercise. This means that if you want to leave a comment, you'll need to complete the calculation before you hit the Post Comment button.
If you want to object to a comment made by another visitor, be sure to contact me and I'll look into it ASAP. Don't forget to mention the page link, or the Doc ID of the document.