Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
|
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 19th, 2004, 12:53 AM
Authorized User
 
Join Date: Jul 2004
Posts: 88
Thanks: 0
Thanked 0 Times in 0 Posts
Default Trapping click of table column at runtime

Hi..

Am creating a table dynamically using vbscript (asp.net). The row ids and column ids are created at runtime. On the click of a column,
a javascript function is called. In this function i successfully trapped the column which was clicked. But i need to trap the column text. How do i do this? The partial code is here:


For row = 0 To dt.Rows.Count - 1 'displaying rows
Response.Write("<tr id=RowId" & row+1 & " onclick=TrapId(id)>")

For col = 0 To dt.Columns.Count - 1 'displaying columns
     Response.Write("<TD id=ColId" & col+1 & " onclick=TrapId(id)>" & dt.Rows(row)(col).ToString & "</TD>")
    
Next
Response.Write("</TR>")
Next


PLease help! Thanks in advance.

:)Spacy
 
Old July 19th, 2004, 08:49 AM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

In the JavaScript function TrapId add this:

var this_ones_text = document.getElementById(id).innerHTML;

This is assuming the temporary ID variable is simply called 'id'.

HTH,

Snib

<><
 
Old July 20th, 2004, 06:13 AM
Authorized User
 
Join Date: Jul 2004
Posts: 88
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Snib. But i have a problem. When i write document.getElementById(id).innerHTML it gives we the contents of the entire row. Even while using innerText it gives the entire row contents whereas i want to trap only the column value where clicked. PLease help!!!

 
Old July 20th, 2004, 06:53 AM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 683
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi Spacy,

This should work in *most* browsers:

Code:
function AlertColText(cell){
    var colText = "";
    var rows = cell.parentNode.parentNode.getElementsByTagName("tr");
    for(var i = 0; i < rows.length; i++){
        var cells = rows.item(i).getElementsByTagName("td");
        colText += cells.item(cell.cellIndex).innerHTML + "\n";
    }
    alert(colText);    
}
then just add:

Code:
onclick="AlertColText(this);"
to your td elements.

Best regards,

Chris

 
Old July 20th, 2004, 07:55 AM
Authorized User
 
Join Date: Jul 2004
Posts: 88
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks a lot buddy!!!!
That helped!

Spacy






Similar Threads
Thread Thread Starter Forum Replies Last Post
Runtime Column Header Text vinod_yadav1919 BOOK: Professional SQL Server Reporting Services ISBN: 0-7645-6878-7 1 November 29th, 2008 01:53 AM
Runtime Column monika.vasvani ASP.NET 1.0 and 1.1 Professional 2 October 25th, 2006 08:53 AM
keep adding table rows on button click wlin ASP.NET 2.0 Basics 0 September 26th, 2006 02:14 PM
right click of table node from server explorer mcslain BOOK: Beginning VB.NET Databases 4 November 7th, 2005 07:59 AM
how to make column of table 1 = to column of table gilgalbiblewheel Classic ASP Databases 4 October 11th, 2004 11:57 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.