Solved

Call function when a table row is clicked

Posted on 2012-03-21
15
306 Views
Last Modified: 2012-03-25
Please let me know how I can call a function when a row in a table is clicked. The function accepts the row number as a parameter.

Thanks!
0
Comment
Question by:dshrenik
  • 5
  • 5
  • 4
15 Comments
 
LVL 31

Accepted Solution

by:
GwynforWeb earned 250 total points
Comment Utility
try this

<table id="myTable" border="1" width="309" height="166">
  <tr>
    <td width="154" height="55">0</td>
    <td width="155" height="55">0</td>
  </tr>
  <tr>
    <td width="154" height="55">1</td>
    <td width="155" height="55">1</td>
  </tr>
  <tr>
    <td width="154" height="56">2</td>
    <td width="155" height="56">2</td>
  </tr>
</table>


<p><input type="button" value="click me"
onclick="document.getElementById('myTable').rows[1].onclick=function (){alert('hello world')}">
0
 

Author Comment

by:dshrenik
Comment Utility
I want the function to be called when the user clicks anywhere on the row, not a specific button. Thanks!
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
test page : http://jsfiddle.net/aPqyj/1/
window.onload = function() {
    var rows = document.getElementById("myTable").getElementsByTagName("tr");
    for(var i=0;i<rows.length;i++) rows[i].onclick = new Function("myfunction(" + i + ")");
}
    function myfunction(n) {
alert("you click on row number " + n);                
    }
¿

Open in new window

0
 

Author Comment

by:dshrenik
Comment Utility
If I have already defined the function as "functionA()" elsewhere in the code, please let me know how I can use that. Thanks!
0
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 250 total points
Comment Utility
window.onload = function() {
    var rows = document.getElementById("myTable").getElementsByTagName("tr");
    for(var i=0;i<rows.length;i++) rows[i].onclick = new Function("functionA(" + i + ")");
}

Open in new window

0
 

Author Comment

by:dshrenik
Comment Utility
Thanks! Does this work only with window.onload?
What if I create my table dynamically?
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
>What if I create my table dynamically?

run the code inside window.onload function immediately after you create it and append it to the page
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 31

Expert Comment

by:GwynforWeb
Comment Utility
dshrenik

My example shows you how to set the row to be clickable.

(1) when page opened the the middle row is not clickable

(2) Press the button and the row in now clickable. and alerts "hello world"

I am trying to illustrating the principle of how to set a row to be clickable , I am not suggesting you have a button in your application.

Basically the statement

document.getElementById('myTable').rows[1].onclick=function (){alert('hello world')}"

will set the row to be clickable. It is up to you where you put it.
0
 
LVL 31

Expert Comment

by:GwynforWeb
Comment Utility
...wow
0
 

Author Comment

by:dshrenik
Comment Utility
@ GwynforWeb:
Sorry about that.

If leakim971 does not have a problem, I will reopen the question and share the points.
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
np
0
 
LVL 31

Expert Comment

by:GwynforWeb
Comment Utility
@dshrenik, That is very kind of you, and you leakim971 as well.
0
 

Author Comment

by:dshrenik
Comment Utility
0
 
LVL 31

Expert Comment

by:GwynforWeb
Comment Utility
ty so much appreciated.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses four methods for overlaying images in a container on a web page
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now