1

I have a simple script im testing which calls a HTML file. In that html file is a div with the id "test2".

I am then trying to add content to its innerHTML but it acts like it don't exist, even though the div shows in the browser.

This is how I have approached the idea:

<script>

function call_file(file,div_id){
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById(div_id).innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET",file,true);
xmlhttp.send();

}

window.onload = function() {  

  file = 'test.html';
  div_id = 'test';
  call_file(file,div_id);
  document.getElementById('test2').innerHTML = 'Hi';
};
</script>
  <div id="test" class="outer""></div>

Content of "test.html":

<div id="test2" class="inner"></div>

Why does it not find id "test2" with this error, even though i see the div with my own eyes:

Cannot set property 'innerHTML' of null 
3
  • try this Commented Oct 15, 2012 at 5:28
  • You could also try placing the script AFTER the div. That way it only runs after the DIV has been loaded into the DOM. Another way to go is to use the $(document).ready() function in jQuery. That only calls after all of the DOM objects have been loaded. Commented Nov 19, 2012 at 23:12
  • No need a window onload deals with all that. Commented Nov 19, 2012 at 23:36

2 Answers 2

2

You're making an asynchronous call, so send() will return before the response is received and added into the DOM. Any code that depends on the received data should be in your onreadystatechange function, or somehow triggered by it, so that it won't execute until the response data has been received.

Sign up to request clarification or add additional context in comments.

6 Comments

I tried this as posted in the other answer: paste.to/MTQwNzY4OA== so it would call a different function once it was ready =/ but not working
Line 31 in your paste.to code is still looking for a DOM element that may not exist yet.
What can i add on line 31 to check the function previous completed correctly tho ?
There are lots of possible ways, the easiest change to your current code would just be to move that into call_back()
Hmm that would limit the options for that function,, is there way to return a true/false and use a if statement to then call line 31?
|
1

You Cannot set it because it doesn't exist, yet. You need to wait until it's loaded.

You can do that with a callback function.

function call_file(file,div_id, callback){
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById(div_id).innerHTML=xmlhttp.responseText;
    return callback();
    }
  }
xmlhttp.open("GET",file,true);
xmlhttp.send();

}

window.onload = function() {  

  file = 'test.html';
  div_id = 'test';
  call_file(file,div_id, function () {
   document.getElementById('test2').innerHTML = 'Hi';
  });
};

1 Comment

Hmm im trying to make it bit more flexible by calling a function instead of return like this: paste.to/MTQwNzY4OA== but the issue remains what should i alter?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.