I am dynamically generating a form. For simplicity’s sake assume it’s a login form with email/password. The form is submitted, but onsubmit it fires an AJAX request that handles the actual login and the submit event is cancelled (e.preventDefault()).
I use e.preventDefault() to cancel the default action of the form, that is, go to the page in ‘action’ but this also seems to cancel the autocomplete detection of the browser.
I think you need to fullfill several requirements for the native autocomplete to work:
- Your input field
type="text"must have aname - The form must be submitted <– this isn’t really happening in my case
Is my analysis correct and is there any way to make autocomplete work in this case?
To ward off the fanboys: I’m not looking for any solution that involves jQuery or [insert your framework], I want to use the native browser autocomplete feature. I don’t have a list of words that I want to autocomplete with.
DMoses solution greatly inspired my solution but it there is a significant difference so I thought it would be a good idea to make my own solution, the bounty goes to DMoses though 😛
DMoses solution moves (or copies) the form to the iframe and then submits it. The reason you want to do this is so your ‘parent’ from doesn’t reload. There is a simpler solution: have the form submit to the iframe. This works just the same and you don’t have to copy any nodes.
This step is entirely repeatable as well. The only downside is that you don’t control when exactly an autocomplete entry is added. You might want to add only valid entries but at least this case mimics perfectly the way a normal form would behave if no ajax were involved. If you want to control what gets added to the autocomplete, use DMoses’ solution, copy the form and submit it in the iframe.
For me, this is enough:
The best part: no extra JavaScript is required to make this work! (other than generating a unique id/name for the form, but that’s super trivial).
jsFiddle: http://jsfiddle.net/KzF6s/13/