0
document.getElementById('menu_link').addEventListener('click', show_menu);
document.getElementById('close_menu').addEventListener('click', hide_menu);
function show_menu () {
    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange=function()
    {
        if (xhr.readyState==4)
        {
            document.getElementById('menu_call').innerHTML = xhr.response;
        }
    }
    xhr.open('GET', "menu.html", false);
    xhr.send(); 
    document.getElementById('menu').classList.remove('hidden');
}

Hi Im doing a one page site and trying to load the content using ajax but i have troubles when im trying to use de ids located in the external htmls in my js script. I hope this part of code shows what i mean.

close_menu div is inside menu.html and i can only reach to it if I put

document.getElementById('menu_link').addEventListener('click', show_menu);

inside function show_menu () { ... } after xhr.send() for this specific case I can do it this way but as the web gets more complex it gets messy so I was wondering if i can access to all id loaded using ajax from any part of the js code.

Thanks!

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • You can only access elements that are actually part of the DOM, so the answer is "no". – Pointy Oct 01 '14 at 17:38
  • possible duplicate of [How to return the response from an Ajax call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call) – André Dion Oct 01 '14 at 17:40

0 Answers0