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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Show/Hide table

Status
Not open for further replies.

flyingchoc

Programmer
Sep 14, 2006
2
GB
Hello,

I am trying to hide an HTML table when the page initially loads and then display it when a button is clicked.
I have created two functions to do this

Code:
function showTable(){

   document.getElementById('myTable').style.visibility = "visible";

}

function hideTable(){
     document.getElementById('myTable').style.visibility = "hidden";

}

Now if I run the hideTable function in the HTML head tag like this:
Code:
<html>
<head>
<script language="JavaScript">
hideTable();
</script>
...

I get an "getElementById has no properties" error.
If I place it after the line of HTML that creates the table the error goes away but the table always remains hidden.

Any ideas anyone?

Thanks
 
[tt]<script language="JavaScript">
[red]//[/red]hideTable();
window.onload=hideTable;
/*
//or
window.onload=function() {
hideTable();
//other stuff
}
*/
</script>[/tt]
Until the page is loaded pass the table element, it is undefined. So execute function on it until the page is loaded.
 
Hey thanks for your help, that managed to cure the problem. But now when I press button and run a piece of code called "showTable" (to display the table) it remains hidden...I have placed some alerts in the code to see where it is going wrong and it seems that after the button is pressed,the table is displayed(ie showTable is executed) and then hidden again. Below is the code..
Code:
window.onload = function(){
   var s = document.getElementById("myTable");
   s.style.visibility = "hidden";

}

function showTable(){
  alert("hello");
  document.getElementById("myTable").style.visibility= "visible"

}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top