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

Test if Table ID exists

Status
Not open for further replies.

Moebius01

Programmer
Oct 27, 2000
309
US
Is there a way to check if a table exists before running code?

I have a handful of pages in a site that all have the same table on them (same table id). I'd like to have my include file run a function on the table if it exists on the page being loaded. If not, the function should stop. Is there a way to achieve this?
 
Sure, just use an if statment like so:
Code:
<html>
<script language="JavaScript">

function checkForTables() {

   //check for the first table
   if (document.getElementById("table1")) {
      alert("table1 exists");
   }
   else {
      alert("table1 does not exist");
   }

   //check for the second table
   if (document.getElementById("table2")) {
      alert("table2 exists");
   }
   else {
      alert("table2 does not exist");
   }

}

</script>
<body onload='checkForTables()'>
<form name=blahForm>

<table id=table1>
<tr><td>This is table 1</td></tr>
</table>

</form>
</body>
</html>

-kaht

Weaseling out of things is important to learn. It's what separates us from the animals... except the weasel. - Homer Simpson (no, I'm not Homer)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top