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

Douglas picture Douglas · Feb 21, 2012 · Viewed 24.8k times · Source

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.

Answer

James Allardice picture James Allardice · Feb 21, 2012

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.