I'd would like in a form with an input control, that the first autocomplete value is visible in the textbox instead of a blank field.

Is it possible in javascript ?

Thanks a lot

Dani AI

Generated

Short answer: you cannot read or force the browser to expose its built-in autocomplete/autofill entries from JavaScript. The saved suggestions are part of the user agent UI and not exposed in the DOM for privacy/security reasons, so there is no standard API to enumerate or preselect the browser's stored values (as noted). See the HTML autofill/autocomplete documentation for background: autocomplete attribute and the spec overview (WHATWG Autofill).

That said, the practical alternatives are straightforward. ’s idea of prefilling a value on load is valid for supplyable defaults, but it does not access the browser’s saved list. Two better approaches are:

  • Use an HTML5 datalist or a JavaScript typeahead so the page supplies the suggestion list (see datalist element).
  • Implement your own suggestion store (localStorage or server-side) and populate/show suggestions under your control. To mimic “inline” completion, set the input value to the suggested text and select the remainder so typing replaces it.

Example pattern (modern approach):

<input id="q" list="saved" autocomplete="off" placeholder="Start typing">
<datalist id="saved">
  <option value="alice">
  <option value="bob">
</datalist>

<script>
const input = document.getElementById('q');
const first = document.querySelector('#saved option');
if (first) {
  input.value = first.value;
  input.setSelectionRange(0, first.value.length);
}
</script>

Notes and cautions: placeholder text is not the same as a prefilled value; browsers may ignore autocomplete="off"; synthetic key events or external hacks (SendKeys) are unreliable and insecure. For cross-browser, accessible behavior, implement and test a controlled suggestion UI (datalist or custom typeahead) rather than trying to manipulate the browser’s private autocomplete store. See MDN for setSelectionRange usage: setSelectionRange.

Recommended Answers

All 3 Replies

'this what you need?

<HTML>
<HEAD>
<SCRIPT TYPE="text/javascript">
function insertvalue()
{
document.formname.sometext.value = "default value";
}
</SCRIPT>
</HEAD>

<BODY BGCOLOR="#FFFFFF" onload="insertvalue()">

<FORM NAME="formname">
<INPUT TYPE="text" NAME="sometext">
</FORM>
</BODY>
</HTML>

Thank for you replly but ...
No no,
I'd like to put in the box the first value available with autocomplete.
You know, when you click on a box if your autocomplete is turned on, the browser propose you the stored values in a list.
Thanks

As I search for it I only found a solution in VB with sendkeys function.
But I'd prefer in Java, asp (don't think a server-side could do it), or html.

The "autocomplete" is an often very annoying and disruptive "feature" of a particular browser or browser plug-in (like the Google Toolbar). As far as I know, a standardized autocomplete was never made part of the JavaScript DOM, so you can't do what you're trying to do.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.