Weird Thoughts From Eric's Head

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

Bird Brains
One day a man goes to a pet shop to buy a parrot. The assistant takes the man to the parrot section and asks the man to choose one. The man asks, "How much is the yellow one?"

The assistant says, "$2000." The man is shocked and asks the assistant why it's so expensive. The assistant explains, "This parrot is a very special one. He knows typewriting and can type really fast."

"What about the green one?" the man asks.

The assistant says, "He costs $5000 because he knows typewriting and can answer incoming telephone calls and takes notes."

"What about the red one?" the man asks.

The assistant says, "That one's $10,000."

The man says, "What does HE do?"

The assistant says, "I don't know, but the other two call him boss."

Make table cells and rows clickable
I have been looking through my logs and I see that a good amount of people do searches on Google for making a table row clickable or a table cell clickable and come to my site. The link they are going to does not explain it well. So I thought I would do a few quick examples and show you how to add some other effects to make it more dynamic.

The first thing you need to do is add an onclick handler to the element. If it is a cell you add it to the td tag. If it is the row, you add it to the tr tag.

This is a basic example to make a table cell clickable:

Test Test - Click Test Test

Code:
<table id="test" border='1' cellspacing='0' cellpadding='0'>
  <tr onclick="alert('bang')">
    <td>Test</td>
    <td>Test</td>
    <td>Test</td>
    <td>Test</td>
  </tr>
</table>

Here is an example to make a table row clickable:
Test Test Test Test

Code:
<table id="test" border='1' cellspacing='0' cellpadding='0'>
  <tr onclick="alert('bang')">
    <td>Test</td>
    <td>Test</td>
    <td>Test</td>
    <td>Test</td>
  </tr>
</table>

Now how is a person supposed to know that they can click on the cell or the row? It is hard since the cursor stays the same and there are no other visual markers. You can change that by changing the cursor and the background color when the mouse is placed on the row or cell.

Example with table cell:
Test Test - Click Test Test

Code:
<table id="test" border='1' cellspacing='0' cellpadding='0'>
  <tr>
    <td>Test</td>
    <td onmouseover="this.style.background='red';this.style.cursor='pointer'"
        onmouseout="this.style.background='white';" onclick="alert('bang')">
        Test - Click</td>
    <td>Test</td>
    <td>Test</td>
  </tr>
</table>

Example with table row:
Test Test Test Test

Code:
<table id="test" border='1' cellspacing='0' cellpadding='0'>
  <tr onmouseover="this.style.background='red';this.style.cursor='pointer'"
        onmouseout="this.style.background='white';" onclick="alert('bang')">
    <td>Test</td>
    <td>Test</td>
    <td>Test</td>
    <td>Test</td>
  </tr>
</table>

Well I hope this helped you out!
Eric Pascarello
Moderator of HTML/JavaScript at www.JavaRanch.com
Author of: JavaScript: Your Visual Blueprint for Dynamic Web Pages


excellent explanation! Thanks a lot.
Exactly what I was looking for. Thanks much!
Perfect...you are a saviour!! My GOD bless you!
I have added another post about this topic where it converts a single hyperlink column and makes the whole row a hyperlink.

Convert Table to Hyperlinks

Eric
Thanks, Eric. You saved me a lot of time.
I am having touble getting it work when I call a function rather then just "alert('text')" I created a function in a linked js file like this: function link(){ window.location="http://www.yahoo.com"; } and on the table I would like to link the row to that location: tr onclick="link()" pretty simple, ahy? It will not work! Thanks for any help, Amber
I have no trouble:
<script type="text/javascript">
  function link(){
    window.location.href="http://www.yahoo.com"; 
  } 
</script>
<table id="test" border='1' cellspacing='0' cellpadding='0'>
  <tr onmouseover="this.style.background='red';this.style.cursor='pointer'"
        onmouseout="this.style.background='white';" onclick="link()">
    <td>Test</td>
    <td>Test</td>
    <td>Test</td>
    <td>Test</td>
  </tr>
</table>
Eric
Eric,

Great code. I have found that you can use this code in the table tag itself while using image rollovers for the entire table:

<script type="text/javascript">
  function link(){
    window.location.href="http://www.yahoo.com"; 
  } 
</script>

<style type="text/css">
<!--
.on {background-image: url(images/image1.jpg); }
.off {background-image: url(images/image2.jpg); }
-->
</style>

<body>
<table class="off" onMouseOver="this.className='on'" 
onMouseOut="this.className='off'" width="800" height="40" border="0" cellpadding="0" cellspacing="0" onclick="link()">
 <tr>
  <td>some content</td>
 </tr>
<table>
</body>


I would be interested in talking to you about a coding issue I am having involving .php amd mySQL using the above code. If you are interested, please email me and I will give you my number, or you can give me yours.

Thanks,

Doug
Thank you for this code! Saved me the time of having to figure it out by trial and error!
Cheers!
Thanks, just the information I was after
Thanks, but how to tell which row was clicked - I need to get the values in the clicked row. Pls help
Genius! Many thanks!
Wonderful tip!! I needed it to pop up a window with a website so I added in the TR code:

link="www.google.com"

and changed the onclick to:

onclick="window.open(this.link,'newwindow', 'width=475,height=400,scrollbars=yes,screenX=190,screenY=130,left=190,top=130');return false;"


Add a comment

Title
Body
HTML : b, i, blockquote, br, p, pre, a href="", ul, ol, li
Math Quiz 2 + 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=1104419159000