Verdemis
Verdemis

Reputation: 287

JQuery eventhandler in external js file doen't work

i'm not sure if im doing something wrong or if it actually doesn't work this way.

i have a jquery eventhandler like

$('.element').on('click',function(){
   //do something
});

this code comes right before my </body> tag. When i write the code between <script></script> tags it works just fine. But when i put the code in an external js file and add it with <script src="myscript.js"></script> at the same place, it doesn't work.

Upvotes: 0

Views: 35

Answers (1)

Gvidas
Gvidas

Reputation: 1964

I guess the problem is your external file is loaded in header & before DOM .element created. You need to place your external file link in footer, before </body> tag. Or wrap your code with

    $( document ).ready(function() {
         $('.element').on('click',function(){
           //do something
       });
   });

Upvotes: 2

Related Questions