1

I have loaded a html file and "placed" it inside a div:

document.getElementById('my_div').innerHTML='<object type="text/html" data="table.html" ></object>';  

Works fine, but when I try to get an element by it's ID in the table (from table.html) I get null/undefined.

var table = document.getElementById('my_table'); /* Nope! */

I guess I am doing it at the wrong time or place somehow. Can you help me please?

Best Regards

1
  • its because your table.html has not been loaded yet. you can move the var table retrieval inside a javascript defined in table.html or use JQuery Commented Apr 24, 2014 at 0:41

1 Answer 1

1

I would do this with jQuery's built-in AJAX method, .load(). Like so:

$(document).ready(function(){
    $('#my_div_id').load('/filepath/table.html');
    var table = $('#my_table');
    // Other code, presumably :)
});
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, will try. I guess your example works only if in the document.ready-function? Otherwise it's no different from mine except the way to load the file.
Yes. It may be that your browser has a cached version of table.html which doesn't have the element with the ID you're trying to get. Try a hard refresh?

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.