7

I am using Google Autocomplete and am getting this error

InvalidValueError: not an instance of HTMLInputElement

I think Google has getElementById for deprication

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

However i'm unsure on it's usage. My code 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);
ottz0
  • 2,595
  • 7
  • 25
  • 45

5 Answers5

2

Use onFocus="functionName()" which calls google.maps.autocomplete on input tag.

Misa Lazovic
  • 2,805
  • 10
  • 32
  • 38
0

I also had this problem and it worked using this

  let searchInputField = document.getElementsByClassName('search-form')[0].getElementsByTagName('input')[0]

  let autocomplete = new google.maps.places.Autocomplete(searchInputField);
Kerox
  • 590
  • 1
  • 8
  • 14
0

When you get the element it returns an array, so:

    const memberPlace = $("#place");
    const memberAddress = $("#address");
    const options = {
        fields: ["address_components", "geometry", "icon", "name"],
        strictBounds: false,
        types: ["establishment"],
    };

    if (memberPlace.length) {
        new google.maps.places.Autocomplete(memberPlace[0], options);
    }

    if (memberAddress.length) {
        new google.maps.places.Autocomplete(memberAddress[0], options);
    }
-1

check if the your function is in $(document).ready function or your document.getElementById won't be found by js (cos it tries to find it before it appears in document)

Aleksey Spirin
  • 170
  • 2
  • 8
-2

It's because you overwrite your input variable. Should work if you just use var input = /** @type {!HTMLInputElement} */( document.getElementById('pac-input')); or replace "pac-input" with "destination"

user2718671
  • 2,866
  • 9
  • 49
  • 86