0

Basically what the question says. I import HTML code from an external file and append it to my main html code:

/**
 * Get the HTML contents from the URL specified in the filename and add them
 * to the before the bottom of the specified DOM element.
 * @param {string} filename The URL of the HTML file we want to import.
 * @param {DOM element} element The element to which we'll append the HTML code.
 */
function importHtml(filename, element) {
    let data = fetch(filename)
    .then(function(response) {
        // When the page is loaded convert it to text
        return response.text()
    })
    .then(function(html) {
        // Return the HTML contents of the file.
        element.insertAdjacentHTML('beforeend', html);
    })
    .catch(function(err) {  
        console.log('Failed to fetch page: ', err);  
    });
    return data;
}
importHtml("html/includes/header.html", header);

I've tried to get access to that HTML code using:

document.addEventListener("DOMContentLoaded", function () {
   const menu = document.querySelector('#menubar');
}

but it doesn't work. It has to be PURE JS.

I've looked but I can't seem to find the answer :/

Nikitas IO
  • 696
  • 5
  • 20
  • 2
    There is not enough info here to help you here. Can you create a [small demo](https://stackoverflow.com/help/minimal-reproducible-example) for this using [jsfiddle](https://jsfiddle.net/) or [snippet](https://meta.stackoverflow.com/a/358993/1823841) here to show the issue happening. – palaѕн Apr 18 '20 at 05:20
  • It is the same question as [this](https://stackoverflow.com/questions/16107267/getting-access-to-a-jquery-element-that-was-just-appended-to-the-dom) but with **PURE JS** instead. – Nikitas IO Apr 18 '20 at 05:27
  • Also my html element is imported from an HMTL file – Nikitas IO Apr 18 '20 at 05:29
  • "_I import HTML code from an external file and append it to my main html code._" can you show the code for it in your post? – palaѕн Apr 18 '20 at 05:33
  • @palaѕн how about now? – Nikitas IO Apr 18 '20 at 05:56

0 Answers0