HTML autocomplete autocomplete error

I use Google Autocomplete and get this error

InvalidValueError: not an instance of HTMLInputElement

I think Google has getElementById for deprivation

var input = /** @type {!HTMLInputElement} */(
  document.getElementById('pac-input'));

However, I am not sure about its use. My code is below

var options ={
    types:['(cities)'],
};

var input = document.getElementById('destination');
var autocomplete = new google.maps.places.Autocomplete(input, options);
}

google.maps.event.addDomListener(window, 'load', initialize);
+4
source share
3 answers

Use onFocus="functionName()"that calls google.maps.autocompleteon the input tag.

+1
source

check if your function is in the $ (document) .ready function or your document.getElementById will not be found by js (because it tries to find it before it appears in the document)

-1
source

This is because you are overwriting your input variable. Should work if you just use var input = / ** @type {! HTMLInputElement} * / (document.getElementById ('Pac-in')); or replace "pac-input" with "destination"

-2
source

Source: https://habr.com/ru/post/1620571/


All Articles