I have a wordpress site, and I have enqued select2.min.js and from what I can see it is loading correctly in the footer. Then I have another js file (app.js) where I put all my jquery/js.
But when i try to Initialize select2 with: $(‘select’).select2(); all jquery/js code below this one stops working and I get the “Uncaught TypeError: Cannot read property ‘select2’ of undefined” in select2.min.js… in the console.
I have no idea why i get this message and how to solve it. The irritating part is it worked before, with the exact same code, but now i upgraded to the latest version (not sure which one i used before) and now i get these problems.
Can’t really say without code or jsfiddle to look at. Are you sure that everything is loading in the correct order?
It could also be possible that the JS is breaking somewhere prior to the loading of select2.min.js or jquery, preventing the load, but the error message isn’t showing until that point.
here is a fiddle, however here I don’t get the issue, but all the other jquery is there. I am a beginner att js/jquery so it might be lots of issues with my code so feel free to tell me.
On the real site everything below the select2-intialilize (which is at the top of the code) breaks. On the real site i also load flexslider and fastcklick before select2 in the functions file.
I hate to say this, but the more things that you load, the chance increases that something is colliding globally. Do you really need all that? Just sayin’.
I tried removing everything in my app.js file except the select2-initialize … still the same problem. Can it have something to do with in what order I load my other jquery plugins? Flexslider, fastclick etc.
Is the CDN the latest version? It might have been a compatibility issue. IDK. I try to use as much vanilla JS as I can, and resort to libraries only when absolutely necessary.