0

I'm trying to load the contents of a HTML file into a div using JavaScript/jQuery by doing this on page load:

$("#div_name").load("xxx.html");

It displays everything fine, but then if I then try to select any of the DOM elements loaded by this file e.g. with document.getElementById(#some_other_div) , they are not found.

I could read in the file as text using AJAX, then use $("#div").append, but it seems pretty long-winded - is there a straight-forward way of making these external elements accessible by the current document or am I going to have to AJAX it?

Magg G.
  • 229
  • 3
  • 10
  • http://stackoverflow.com/questions/19370417/how-to-load-external-html-into-a-div – pattmorter Apr 15 '14 at 20:52
  • How, exactly, are you trying to select the new AJAX-loaded DOM elements? Do they have duplicate IDs? Are you using [event delegation](http://learn.jquery.com/events/event-delegation/) to act on them? You need to share more code with us for a definitive answer. – Blazemonger Apr 15 '14 at 20:53
  • 1
    Are you trying to access these elements right after calling `load()` instead of in the callback? – Jason P Apr 15 '14 at 20:54
  • 1
    Also, `.load()` is just an ajax shorthand method. It uses ajax behind the scenes. – Jason P Apr 15 '14 at 20:55
  • @Jason ah, yes, I was calling another function right after `load()` to change attributes of them - I just tested it by calling the same function on an `onclick` event and it works now. I can't believe it was that easy! – Magg G. Apr 15 '14 at 21:02
  • also, I didn't know that about `load()`, thanks for the info – Magg G. Apr 15 '14 at 21:02

0 Answers0