Quantcast
Channel: Active questions tagged html - Stack Overflow
Viewing all articles
Browse latest Browse all 73992

Why is javascript not working when I load a html page dynamically? (No jquery) [duplicate]

$
0
0

This question already has an answer here:

When i click a button on index.html to load test.html dynamically, the javascript which is written in test.html does not work.

test.html below.

<p>Test for dinamic loaded javascript</p>
<button id="testButton"></button>

<script>
    const testElem = document.querySelector('#testButton');
    testElem.addEventListener('click', testFunction);

    function testFunction() {
        alert('Hello World');
    }
</script>

the script below loads test.html dynamically.

fetch('/test.html').then(function(response) {
  return response.text();
}).then(function(string) {
  const laden = document.querySelector("#demo");
  laden.innerHTML = string;
  console.log(string);
}).catch(function(err) {  
  console.log('Fetch Error', err);  
});

Viewing all articles
Browse latest Browse all 73992

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>