Kevin Powell - CommunityKP-C
Kevin Powell - Community15mo ago
7 replies
Faker

How to properly call a function in an event handler

Hello guys, sorry to disturb you all; I'm getting an error on line 7 but I didn't understand why, can someone explain why please

const btn = document.querySelector('#__btn');
const text = document.querySelector('.content');
const source = 'scripts/callbacks.js';

btn.addEventListener('click', (loadScript) => {
    text.textContent = 'Loading another JS Script . . .';
    loadScript(source ,sayHello);

})


function loadScript (src, sayHello) {
    const script = document.createElement('script');
    script.src = src
    script.onload = sayHello;
    document.head.append(script);
    console.log('should be executed first followed by callback');
}

function sayHello() {
    console.log('Hello world !');
}
039AF31F-0B62-4BB6-AC1B-267AEA1B84EB.png
Was this page helpful?