Change default text "Enter a Location" In Google Maps Places API

27,151

Just set the placeholder attribute of the input element to which you are attaching the autocomplete to whatever you want:

<input type="text" placeholder="Some new text">

This is detailed in the documentation:

By default, the text field created by the Autocomplete service contains standard placeholder text. To modify the text, set the placeholder attribute on the input element.

Share:
27,151
Douglas
Author by

Douglas

Front End Web Developer

Updated on September 04, 2020

Comments

  • Douglas
    Douglas over 3 years

    I've looked all over the internet trying to find an answer to my problem, and the closest thing I've found to an answer is another Stack Overflow question, here: How do i change the default text in Google Maps API Places Autocomplete

    The problem is that either the OP did not have quite the same requirements as myself, or they didn't quite make it clear if they do.

    I'm looking for a way to take the default "Enter a Location" in the text input field, and replace it with something else while keeping the same functionality. i.e. have the text disappear on focus, and reappear when it loses focus if no text was entered.

    In my current attempt, I've tried to replace the text using a jQuery plugin. It works visually on pageload, but when clicked, "Enter a Location" pops up again, and disappears when text is entered, or the input loses focus (as opposed to the text entry field going blank as intended).

    Can anyone give me a pointer here?

    Thanks in advance.

  • Douglas
    Douglas about 12 years
    That did it. Thanks. I can't believe I missed it way down at the bottom of the page there. I guess I wasn't using the right keywords to search.
  • James Allardice
    James Allardice about 12 years
    No problem, glad I could help. You may want to test it and see what happens in browsers that don't support the placeholder attribute (IE8 and below). I don't know what will happen there.
  • Douglas
    Douglas about 12 years
    There's one minor hitch, where if the user enters text into the field, then removes it, it remains blank when it loses focus (as opposed to returning to the default I set in JQuery). This doesn't bother me too much, but is not the ideal situation.
  • James Allardice
    James Allardice about 12 years
    @Douglas - I think you'll probably have to live with it. Who knows what's going on underneath the hood when you initialize the autocomplete (the default placeholder doesn't behave like a native placeholder, so it's definitely doing something to it). You can't really do any more than stick to the method outlined in the docs.
  • Hitesh Kamani
    Hitesh Kamani almost 8 years
    Is this possible in android app?