51

I have a input field which shows a list using html5 <datalist> element. The problem is that with <datalist> the browser autocomplete also shows the history list (which is the list of previously typed values, that are not included in the <datalist>). So I just want to get rid of the history-list not the <datalist>.

If I use the autocomplete = "off" feature, this also blocks the <datalist>.

In short, I just want the <datalist> not the history one.

2
  • I'm also having this problem. Seems like an oversight not to include a way to disambiguate between these two. Commented Nov 20, 2018 at 16:54
  • 2
    I didn't meet the issue of blocking datalist. Checked under Firefox 63.0.3 and Chromium 70.0.3538.110. Everything works as expected with autocomplete="off" Commented Dec 10, 2018 at 11:01

6 Answers 6

24
+150

Is it possible for you to use the input field without an id or name attribute? Without that, the browser doesn't really have any way to associate a history with that element.

In my real quick testing on Firefox, this seemed to do the trick:

<form>
  <!-- these will remember input -->
  With id: <input id="myInputId" list="myList" /><br />
  With name: <input name="myInputName" list="myList" /><br />

  <!-- these will NOT remember input -->
  No id, name, class: <input list="myList" /><br />
  With class: <input class="myInputClass" list="myList" />

  <datalist id="myList">
    <option value="Option 1"></option>
    <option value="Option 2"></option>
  </datalist>

  <br />

  <input type="submit" />
</form>

In the code above, the inputs with an id or name would remember past values, but the input without anything and the input with just a class would not remember anything.

Unfortunately, this does make using the input slightly more difficult if you need it to have a name or id. In that case, I'd try having an id'ed input which is also display: none'ed and then use some JavaScript to keep it in sync with an input that won't remember past values.

Sign up to request clarification or add additional context in comments.

5 Comments

Ah, good suggestion. In fact, I just realized I actually solved the same problem on my own question by using an <input type="hidden"> and leaving out the name on the real input. I keep them in sync with a simple piece of jquery: stackoverflow.com/questions/29756791/…
@StephanMuller yup, that's exactly what I was thinking!
Interesting suggestion, can someone confirm if the same works for Chrome as well? I presume even chrome remembers history based on ID?
Tested in Chrome and it works as advertised. However, my HTML is generated from JSP and I can't avoid using "name" attribute since it is needed for mapping. Any other solutions?
In the latest versions of Firefox (50) and Chrome (55), using autocomplete="off" on the input has the expected behavior: it disables the history but shows the datalist options.
17

Try using the HTML attribute autocomplete

<input name="q" type="text" autocomplete="off"/>

source Turn Off Autocomplete for Input

5 Comments

Thanks! That'll teach me to read the docs first: developer.mozilla.org/en-US/docs/Web/HTML/Element/input
The op did say that they used autocomplete attribute.
The issue arise in combination with datalist... After you've submited the form, it remembers datalist value previously selected, though autocomplete is "off". I just faced it now under firefox 62.0.3
I found that in Chrome, it was still suggesting wrong things at the bottom depending on the name of the input and/or datalist. Ex. field name medNames showed a few of my name combinations at the bottom. By using the datalist id for the autocomplete value prevented that.
Easiest and best solution. I did not face the problem that OP did; connect input with list = "datlistID". (I also use placeholder for old value)
3

I use a code like this:

<input name="anrede" list="anrede" onmousedown="value = '';" />
<datalist id="anrede">
    <option value="Herr"></option>
    <option value="Frau"></option>
</datalist>

good: The datalist shown for selection is complete

bad : The input field is empty, therefore the old value is not documented, if needed to be remembered by the user

1 Comment

If you want to remember the input value for the user, put the value also in the placeholder="value" - so it's a little help for the user ;-)
3

you can use autocomplete="new-password". autocomplete = "off" didn't worked in my case.

<input name="q" type="text" autocomplete="new-password"/>

1 Comment

This was the best answer as it only shows the actual datalist options AND that which has been typed in that before. But, I was using the name/id of the datalist for the autocomplete value. Not sure where new password came from
1

Try this, I hope it works.

<input id="datalisttestinput" list="stuff" autocomplete="off"></input>
<datalist id="stuff">
     <option id="1" value="Amy">
     <option id="2" value="Kristal">
     <option id="3" value="Collin">
     <option id="4" value="Carl">
</datalist>

Comments

-1

try this:

<datalist id="datalist_id">

js:

dataList = $("#datalist_id")
dataList.empty()

This will clear the datalist's history. This worked for me on chrome. Then if you want to add options to the list, try:

dataList.append("<option>Some Option</option>")

Cheers !!!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.