SEVENELEVEN
SEVENELEVEN

Reputation: 258

Function inside IIFE not working correctly

I have a console.log inside a function in an IIFE that is not working (doesnt show the "id").

The console.log outside the IIFE is working correctly.

I can't find where is the problem.

(function () {
    document.addEventListener("DOMContenLoaded", () => {
        const paramsURL = new URLSearchParams(window.location.search);

        const customerId = parseInt(paramsURL.get("id"));

        console.log(customerId);
    });
})();

console.log(window.location.search)

Upvotes: 0

Views: 62

Answers (2)

Will Walsh
Will Walsh

Reputation: 2054

I suspect the problem is related to a typo. It is DOMContentLoaded not DOMContenLoaded.

(function () {
    document.addEventListener("DOMContentLoaded", () => {
        const paramsURL = new URLSearchParams(window.location.search);

        const customerId = parseInt(paramsURL.get("id"));

        console.log(customerId);
    });
})();

console.log(window.location.search)

Upvotes: 2

CodeWalker
CodeWalker

Reputation: 2378

Instead of using an IIFE, this would simply work.

document.addEventListener("DOMContenLoaded", () => {
    const paramsURL = new URLSearchParams(window.location.search);
    const customerId = parseInt(paramsURL.get("id"));
    console.log(customerId);
});

Upvotes: 1

Related Questions