How to solve $ is not defined

Avatar

Avatar

pillaivinit

Avatar

pillaivinit

pillaivinit

20-12-2020

I have a js function using $ in it.

My function uses $get(). and works fine when this function is called from HTL but if I add the function in document.load 

$(document).load(function () {
    alert("alert");
});

then I get $ is not defined in console.

Only the function which is called  in HTL(Sightly) works.

I'm using AEM 6.3.

AEM 6.3 jquery

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar

Suraj_Kamdi

MVP

Avatar

Suraj_Kamdi

MVP

Suraj_Kamdi
MVP

21-12-2020

@pillaivinit 

First thing, Jquery should always load before the execution of your code.

Second thing, It happens mainly because of your clientlib category /dependencies order.

Answers (2)

Answers (2)

Avatar

Avatar

asutosh_j3

Avatar

asutosh_j3

asutosh_j3

20-12-2020

@pillaivinit You need to make sure the jquery is defined before this line of code is executed.

Avatar

Avatar

Anudeep_Garnepudi

MVP

Avatar

Anudeep_Garnepudi

MVP

Anudeep_Garnepudi
MVP

20-12-2020

Hi @pillaivinit 

In the page where you are getting error, right click on the page and click view source and in source search(Ctrl+F) for jquery. Make sure that jquery loads in head section of your page.

document. is Javascript and will work without error

$(document). $ is jquery framework, we should load it before using it on page.

-AG