Weird Thoughts From Eric's Head

Tags - Categories : All | AJAX | BUSINESS | PERSONAL | PROGRAMMING | BOOK REVIEW

Convert Table Row to Hyperlink
Well I saw a post here on the asp.net forums wanting to take a single column that contains a hyperlink and make the whole row clickable.

Well, I talked about making the whole row clickable here on my blog before, but the user wants to take a hyperlink from one of the columns and do it. A light bulb went off in my head and I coded this in two minutes.

<html>
  <head>
    <style type="text/css">
      table{
        border: 1px solid black;
        border-collapse: collapse;
        width: 50%;
      }
      th, td{
        border: 1px solid black;
        padding: 3px;
        width: 25%;
      }
      th{
        background-color: #A0A0A0;
      }
      tr{
        background-color: white;
      }
      tr.highlight{ 
        background-color: #B8CDDC;
        cursor: pointer;
      }
    </style>
    <script type="text/javascript">
      window.onload = function(){
        ConvertRowsToLinks("table1");
      }
      
      function ConvertRowsToLinks(xTableId){

        var rows = document.getElementById(xTableId).getElementsByTagName("tr");
   
        for(i=0;i<rows.length;i++){
          var link = rows[i].getElementsByTagName("a")
          if(link.length == 1){
            rows[i].onclick = new Function("document.location.href='" + link[0].href + "'");
            rows[i].onmouseover = new Function("this.className='highlight'");
            rows[i].onmouseout = new Function("this.className=''");
          }
        }

      }

    </script>  
  </head>
  <body>
    <table id="table1">
      <tr>
        <th>Website</th>
        <th>Subject</th>
        <th>Content</th>
      </tr>
      <tr>
        <td><a href="http://www.JavaRanch.com">JavaRanch</a></td>
        <td>Java</td>
        <td>Content and Forums</td>
      </tr>
      <tr>
        <td><a href="http://www.asp.net">ASP.NET</a></td>
        <td>.NET</td>
        <td>Content and Forums</td>
      </tr>
      <tr>
        <td><a href="http://radio.javaranch.com/Pascarello">Eric's Blog</a></td>
        <td>JavaScript,.NET,and Stuff</td>
        <td>Programming Discussion</td>
      </tr>
    </table>
  </body>
</html>

Basically we are grabbing the table's rows. We than loop through each of the rows looking for a link. If a link exists, we grab the href and attach an onclick event to the element. To make it fancier, I added some mouse events so the row is highlighted.

Hope this is helps,

Eric Pascarello
Moderator of HTML/JavaScript at www.JavaRanch.com
Author of: JavaScript: Your Visual Blueprint for Dynamic Web Pages


Thank you, it was very useful!!
This is the nuts, save me a lot of time!
i have a question for u...what do i do if i want that link to open in a new window?
rather easy to do change the line:

rows[i].onclick = new Function("document.location.href='" + link[0].href + "'");

to

rows[i].onclick = new Function("var newWin = window.open('" + link[0].href + "')");

Eric
What about more multiple tables on one page
then call the function for each table.
window.onload = function(){
  ConvertRowsToLinks("table1");
  ConvertRowsToLinks("table2");
  ConvertRowsToLinks("table3");

}
Eric
error:document.GetElementByID(...) is Null or Not an Object
I had included the script on my MAINPAGE.ASP while the processed code was transfered to SECONDPAGE.ASP where also the script was included. The error was caused because the SECONDPAGE.ASP was calling the script too??? Cause when I removed the script from the secondpage it stopped giving the error message.
Thank's I was being a bit think, i'd tried that but had a error elsewhere that was stopping it working, Thanks for the relpy ;) I have another question though: I have a table that have two rows per record, but only for some record as it's conditional based on the content of the record. How would I get this conditional row to also be included in the link/rollover behavior?
You would have to do some sort of check with rows[i+1]. Eric
This is exactly what I was looking for, thank you. I am a bit of a javascript noobs, but have a lot of experience creating clean CSS layouts as well as php coding and also have an understanding of the DOM, so it's not much of a jump for me to get into javascript. I updated the function a little bit, so that it keeps existing styles intact and it also changes the window status text so it works like a real link.
rows[i].onmouseover = new Function("this.className='highlight'; window.status='" + link[0].href + "';");
rows[i].onmouseout = new Function("this.className='" + rows[i].className + "'; window.status='';");
Great example code. Exactly what I was looking for.
Great Example..but the only problem is that in ie referrer is not passed. Can you suggest a method so referrer is also passed.
Any way of changing the row color of a visited link?
I was wondering if you know an error about window.load? I'm using Firefox (don't know if that has to do with it) but I put an alert in it to debug the code and it isn't getting called. Any idea?
Just what I was looking for. (my target in the <a href) doesnt work.) If I want to open the link in a new window I use target="_blank" in the a href line. It works without the "convert table row" but not with it. Hope you can explain this
Did you read through th comments? http://radio.javaranch.com/pascarello/2005/05/19/1116509823591.html#comment1120144948690. It was asked on how to open links in new windows.

Eric
Hi Great stuff Question: What if I only want to have the column as a link and not the row ? I have a row with several columns, and I would like each column in the row as a link I am not that good in javascript, so I hope you can help Thanks in advance
Hey, looks great. How can I make the JavaScript external? I'm quite a newbie on the JS-area, but I need this very much.
awesome code, very useful. I had another question for a similar case you probably can help me with. How to highlight a table row when a link is clicked on a cell in the same row?
hello..... i need to enable one column as hyperlinked one to which the data is extracted from databse. if that hyperlink is clicked,a new window has to be opened. please its urgent. expecting ur response soon.
Are you still following this old thread??? If so..is there a way to modify the onclick portion of the function to pass an HREF that refers to another JavaScript to open a popup window. I am using a thumbnail of an image that the users would click and the script will open a popup window with a full version of the image. I have your version working just fine untill I add the javascript HREF then it doesn't "see" the link so it doesn't make the row clickable. TIA if you are still out there.
This worked great! Thank you. I did need to direct the href to an iframe but no problem:

rows[i].onclick = new Function("parent.frames['frame2'].document.location.href='" + link[0].href + "'");
Hi Eric, I love this script. It's easy to understand! But, I have a question. I'm using this for my cms that I'm working on at the time, and it's basicly an interface like PhpMyAdmin: a list of items in a table, with checkboxes in front of each item (<tr>). If I click the row, I want to go to a page that displays all the parts of this item, but I don't want to do this when clicked on a checkbox. So my question is, how can I 'eliminate' 1 column cell from this hyperlink?
Just remove the href from that column and you'll be fine :)
Robbert, Take a look at: http://www.quirksmode.org/js/events_order.html Eric
Hi Eric, I have a little question about it. I have a table which has alterative rows color. For example the first row background color is #FFFFFF, the second row's background color is #AAAAAA, the third row's background color is #FFFFFF, the fourth row's background is #AAAAAA again, and so on. When I use your script, after I move over all of the rows, the rows background will be #FFFFFF. How can we recover all rows' style after mouse move out? Thanks
Very good mate.. it is very very helpful..!!


Add a comment

Title
Body
HTML : b, i, blockquote, br, p, pre, a href="", ul, ol, li
Math Quiz 1 + 10 = (Helps stop blog spam)
Name
E-mail address
Website
Remember me Yes  No 

E-mail addresses are not publicly displayed, so please only leave your e-mail address if you would like to be notified when new comments are added to this blog entry (you can opt-out later).

TrackBack to http://radio.javaranch.com/pascarello/addTrackBack.action?entry=1116509823591