hi guys, this is my first javascript program so i'm certainly a noob.
i just want to ask why doesn't this global variable not recognized by my functions .. ?

the thing is i need to get the id of a certain table to count the number of rows and columns in it so that i could sum up their values
this is the code that i've used in getting the table id:

var Tableid;
function getTableID(tableID){
    while (tableID.nodeName.toLowerCase() != 'table')
        tableID = tableID.parentNode;
    alert(tableID.id);//for checking only
    var Tableid = tableID.id;
}

function getColumnCount() 
{ 
    return document.getElementById(Tableid).getElementsByTagName('tr')[0].getElementsByTagName('td').length; 
} 

function getRowCount() 
{ 
return document.getElementById(Tableid).rows.length; 
} 

to trigger the getTableID i place the onclick="getTableID(this)" on the input tag of html.
i'm pretty sure that the code for the summation is working because i've tried specifying the table name.

thx in advance.

Recommended Answers

All 3 Replies

Where you are setting Tableid, I believe you are actually redeclaring a local variable with the same name, so your global is never getting set. Try removing the var from line 6 and see if that solves your problem.

Here is some general information on javascript variables that may also be helpful:
http://www.w3schools.com/js/js_variables.asp

i have tried removing the var .. but still it doesn't recoginzed the value that getTableID is passing on to it ..

thnx for the idea .. i have fixed the problem already, i initialized the global variable Tableid = getTableID(tableID); and it works fine now ..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.