Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make browser remember form values for autocomplete on ajax submit?

I have created a form, which on submit does a ajax call for the submission. It is not a standard form submit. How can I make browser remember form data(not just username and password) for autocomplete? I have tried submitting the form to hidden iframe but it is only working for chrome. Is there a solution or workaround which can work on all the browser.

like image 871
dzekoo Avatar asked Oct 18 '22 21:10

dzekoo


1 Answers

I finally found the solution to this.

Basically the browser store the input value when the submit event is triggered on the form.

All you need to do is then prevent the page reload by calling the event.preventDefault() function and handle the rest with Javascript.

Here's a basic example : https://codesandbox.io/s/phnsn

<form name="jsForm">
    <input type="text" name="search" autocomplete="search" />
    <button type="submit">Search</button>
</form>

<script>
    document.jsForm.addEventListener("submit", function(event) {
        event.preventDefault();

        // Handle Ajax call here
    });
</script>

Keep in mind that if you added a click event on the submit button, you still need to programmatically trigger the submit event. So it's best to use a button with submit type.

like image 80
SimonDepelchin Avatar answered Oct 20 '22 11:10

SimonDepelchin