Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

215
Views
New with Svelte and using Google Places Autocomplete API

I'm really new with Svelte and I'm trying to make a web component that calls the places autocomplete library of Google. I made it to load the library on my Svelte component, but when I want to pass the autocomplete function to my input it show me the error: InvalidValueError: not an instance of HTMLInputElement.

This is what I tried so far:

<svelte:head>
  <script
    defer
    async
    src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDaZc7Jr7pDoK3TWcDiv-SjqiJ0iKz15Go&libraries=places&callback=initMap">
  </script>

  <script>
    function initMap() {
      const input = document.getElementById("autocomplete");
      const options = {
        componentRestrictions: { country: "us" },
        fields: ["address_components", "geometry", "icon", "name"],
        strictBounds: false,
        types: ["establishment"],
      };
      const autocomplete = new google.maps.places.Autocomplete(input, options);
    }
  </script>
</svelte:head>

This is how my input looks like:

 <input
    type="text"
    id="autocomplete"
    name="store"
    class="pac-target-input"
    value=""
  />
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

It seems like your input hasn't been rendered to the DOM when you call the initMap() method, therefore your input variable is null. Svelte offers a onMount handler which runs after the component is first rendered to the DOM. Inside this handler, you can call your initMap() function:

import { onMount } from 'svelte';

onMount(() => {
    initMap();
});

More details: Svelte Docs

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!