-1

My html code looks like this:

I get an error that $ is undefined.

<html>
<head>
</head>
<body>
<div></div>
<div></div>
<div></div>
<script src="./jquery.js"></script>
<script>
function initialize() {
            var input = document.getElementById('where');
            $(input).on('focus', function() { 
            selected = false;
        });
}
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=blablabla&libraries=places&language=en&region=CA&callback=initialize" async defer></script>
</body>
</html>

I tried removing the async defer, but $ stays undefined.

I tried changing the function initialize() for var initialize = function(), but $ stays undefined.

I tried both, but $ stays undefined.

enter image description here

geocodezip
  • 158,664
  • 13
  • 220
  • 245
Emilio
  • 1,314
  • 2
  • 15
  • 39
  • is jquery being imported correctly? can you make sure from the network tab – cdoshi Aug 09 '18 at 03:20
  • @cdoshi yes, I can use Jquery in the console! – Emilio Aug 09 '18 at 03:21
  • by $(input) if you mean you want to select input elements, wrap it with inverted commas $('input') – cdoshi Aug 09 '18 at 03:26
  • @cdoshi I edited my question, `input` really is a variable. – Emilio Aug 09 '18 at 03:28
  • ok, i see. would be best if you put up a jsfiddle or something similar. code looks ok for $ to work – cdoshi Aug 09 '18 at 03:29
  • Please provide a [mcve] that demonatrates your issue. The posted code gives me an (obvious) invalid key error: `Google Maps JavaScript API error: InvalidKeyMapError`, if I fix that (by removing the key) [it works](http://www.geocodezip.com/SO_20180809_jqueryIncludeIssueNoKey.html) – geocodezip Aug 09 '18 at 12:13

1 Answers1

0

Possibly you are using jQuery in your code before even its getting loaded. So when your jQuery code is rub at that moment '$' has no meaning. You areavle to access later because file is loaded after that point. Check order of your scripts how you are loading files

Monis
  • 165
  • 2
  • 12
  • Thanks, your answer made me think. I found out webpack was doing something crazy to my code. https://stackoverflow.com/questions/29080148/expose-jquery-to-real-window-object-with-webpack – Emilio Aug 09 '18 at 13:42