F
FAQ server
-----------------------------------------------------------------------
FAQ Topic - I get an error when trying to access an
element by getElementById but I know it is in the document.
What is wrong?
-----------------------------------------------------------------------
You cannot access elements that appear in the document after you try
to read them with a DOM method like ` document.getElementById `.
You can either:
A) include your script after the HTML element it refers to, or
B) use the "load" event to trigger your script.
Example A:
<div id="snurgle">here</div>
<script type="text/javascript">
(function(){
var snurgle = document.getElementById('snurgle');
})();
</script>
Example B:
<script type="text/javascript">
window.onload = findElement;
function findElement(){
var snurgle = document.getElementById('snurgle');
};
</script>
</head>
The complete comp.lang.javascript FAQ is at
http://jibbering.com/faq/
FAQ Topic - I get an error when trying to access an
element by getElementById but I know it is in the document.
What is wrong?
-----------------------------------------------------------------------
You cannot access elements that appear in the document after you try
to read them with a DOM method like ` document.getElementById `.
You can either:
A) include your script after the HTML element it refers to, or
B) use the "load" event to trigger your script.
Example A:
<div id="snurgle">here</div>
<script type="text/javascript">
(function(){
var snurgle = document.getElementById('snurgle');
})();
</script>
Example B:
<script type="text/javascript">
window.onload = findElement;
function findElement(){
var snurgle = document.getElementById('snurgle');
};
</script>
</head>
The complete comp.lang.javascript FAQ is at
http://jibbering.com/faq/