Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript problem

Status
Not open for further replies.

sammes

Programmer
Jul 13, 2004
7
GB
Hi,

could anyone help with this simple javascript ?

I habe a webpage which displays several reports.

To avoid mass table rows on opening the page I hide the table rows and only show the headers with a button "show table entries".

This works fine for 3 tables.

This is the script:
<script type="text/javascript">

var rowVisible1 = false;
var rowVisible2 = false;

function toggleDisplay(tbl, no) {

var rowVisible;
if(no == '1'){
rowVisible = rowVisible1;
}
if(no == '2'){
rowVisible = rowVisible2;
}
var table = document.getElementById(tbl);
var tblRows = table.rows;
for (i = 0; i < tblRows.length; i++) {
tblRows.style.display = (rowVisible) ? "none" : "";
}
if(no == '1'){
rowVisible1 = !rowVisible1;
}
if(no == '2'){
rowVisible2 = !rowVisible2;
}
}
</script>

I had to make the 2 variables (rowsVisibleXY), because I had the problem, that when I opened the first table and then when i clicked on the second button to show also the second table I always had to click twice to show it. So this script had a problem to recognize which table is already shown or not etc. and I think that the script thought that it should not open the second table because the first could be openend etc...

So, my question is, HOW can I dynamically generate these rowVisible variables ?

So, I would then pass the number of the table (like I did it currently with 1 or 2) to the script and the script should dynamically create such a variable "rowVisible"+number. And then I would get rid of thes two "if" blocks and I would make a for loop.

Know what I mean ?

cheers
sammes
 
The buttons are inside the tables yes?

If so you can get a handle to the table you are currently in by interrogating the onclick event's srcElement property. This will return the element that triggered the event (the button). By then stepping back up the hierarchy you can get a handle to the table object that ultimately contains that button.

Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.

Enable Apps
 
Aha.

How does this work ?

Could you show me some code ?

I dont know how to change the script for that.

But the Buttons are NOT in the table. The are in a static table above.

So this wouldnt work then...

But what a bout a dynamically generation of these variables ?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top