By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,453 Members | 937 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,453 IT Pros & Developers. It's quick & easy.

Question about javascript scope

P: 1
Hi all,

I'm really new to javascript so this may be a trivial question but I can't seem to find any straight answers. Basically, I have this external js file I'd like add rows to a table in an html page. So for example in my js file, I have something like:

function addRow() {
var theTable = document.getElementById('myTable');
//add new tr, td, etc.
}

and in my html page I have:

<head>
<script ... src="temp.js"></script>
</head>

<table id="myTable">
...
</table>

The problem is whenever I call the javascript function the table variable comes up null. So I'm guessing it didn't find the table. So my question is, is this a scope problem and if so, are there any solutions to this problem. Note, I can't place the function inside script tags inside the head because of another problem I'm having.

Thanks in advance.
Nov 21 '06 #1
Share this Question
Share on Google+
1 Reply


Expert 100+
P: 1,892
At first glance, I think you have some incorrect syntax change:

Expand|Select|Wrap|Line Numbers
  1. var theTable = document.getElementById('myTable');
  2.  
TO
Expand|Select|Wrap|Line Numbers
  1. var theTable = document.getElementById("myTable");
  2.  
If that doesn't do it post again.

HTH,
Aric
Nov 22 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.