Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Call function when a table row is clicked

Posted on 2012-03-21
15
Medium Priority
?
352 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 4
15 Comments
 
LVL 31

Accepted Solution

by:
GwynforWeb earned 1000 total points
ID: 37750337
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
ID: 37750350
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
ID: 37750368
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:dshrenik
ID: 37750377
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 1000 total points
ID: 37750388
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
ID: 37750391
Thanks! Does this work only with window.onload?
What if I create my table dynamically?
0
 
LVL 82

Expert Comment

by:leakim971
ID: 37750400
>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
 
LVL 31

Expert Comment

by:GwynforWeb
ID: 37750415
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
ID: 37750417
...wow
0
 

Author Comment

by:dshrenik
ID: 37750423
@ 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
ID: 37750429
np
0
 
LVL 31

Expert Comment

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

Author Comment

by:dshrenik
ID: 37753865
0
 
LVL 31

Expert Comment

by:GwynforWeb
ID: 37762853
ty so much appreciated.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Australian government abolished Visa 457 earlier this April and this article describes how this decision might affect Australian IT scene and IT experts.
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

604 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