Prevent and deactivate autofill on input fields to optimize user experience

Autocomplete works fine when there is only one input in the form, but as soon as I add more than one input, it stops functioning properly. I am using this form to collect addresses using the Google API, and the issue arises every time I try to add a new address because the previous input keeps appearing.

https://i.sstatic.net/ikRsh.png

<form autocomplete="off" id="form_address" >
    <input type="text" id="postal_code" onFocus="geolocate()">
    <input type="text" id="city">
    <textarea placeholder="Delivery Instructions"></textarea>
</form>  


<script>
    var placeSearch, postal_code;
    var componentForm = {
        locality: 'long_name',
        administrative_area_level_1: 'short_name',
        country: 'long_name',
    };

    function initAutocomplete() {
        postal_code = new google.maps.places.Autocomplete((document.getElementById('postal_code')),
        {types: ['geocode']});
        postal_code.addListener('place_changed', fillInAddress);
    }

    function fillInAddress() {
        var place = postal_code.getPlace();

        for (var component in componentForm) {
            document.getElementById(component).value = '';
            document.getElementById(component).disabled = false;
        }

        for (var i = 0; i < place.address_components.length; i++) {
            var addressType = place.address_components[i].types[0];
            if (componentForm[addressType]) {
                var val = place.address_components[i][componentForm[addressType]];
                document.getElementById(addressType).value = val;
            }
        }
    }

    function geolocate() {
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(function(position) {
            var geolocation = {
                lat: position.coords.latitude,
                lng: position.coords.longitude
            };
            var circle = new google.maps.Circle({
                center: geolocation,
                radius: position.coords.accuracy
            });
            postal_code.setBounds(circle.getBounds());
            });
        }
    }
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDDSzHOBeXL_Goaj4tecH3l0YTJ7anf2rc&libraries=places&callback=initAutocomplete" async defer></script>

Answer №1

To prevent automatic suggestions, it's recommended to disable autocomplete on both the form and input elements.

<form autocomplete="off" id="form_contact">
    <input type="text" autocomplete="off" id="phone_number" onFocus="validateNumber()">
    <input type="email" autocomplete="off" id="email_address">
    <textarea placeholder="Additional Comments"></textarea>
</form>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Utilizing eval properly in JavaScript

One method I am using is to load a different audio file by clicking on different texts within a web page. The jQuery function I have implemented for this purpose is as follows: var audio = document.createElement('audio'); $(".text_sample ...

Align content vertically within a div container

I have encountered an issue where I am trying to center vertically the content within a div. Here is an example of the structure: <div class="container-fluid"> <div class="row restaurant"> <div class="col-md-6"> & ...

Concealing information based on the value of a variable

Creating a dynamic price estimator. I need help writing a jQuery function that can hide or show a specific div element based on the value of a variable. For example, let's say I have an HTML div with the ID 'Answer': <div id="answer"&g ...

Discover ways to retrieve an ajax response from a different domain by submitting specific data to the controller method while operating on an internet server

I am facing an issue where I am unable to retrieve array data from a Codeigniter controller using an Ajax request. The data is being posted to the controller to fetch related data from the database, and it works perfectly fine on my local server. However, ...

Dynamic Navigation Menu: Subcategories Crossing Over Menubar Items

Task Clarification Lately, I've been working on developing a left-side navigation bar for my webpage. This bar consists of two levels of items. When viewed on a smaller device, only the icons of the first level appear. Hovering over these icons trigg ...

Issue with jQuery: Changes are not being triggered and clicks are also not working

I managed to recreate the modifications of my complex script in a simple jsfiddle, which can be found here. Below is the code I am working with locally: HTML <input type="text" id="rangeStart" value="updateMe" /><br/> <input type="text" c ...

Developing User-Friendly Websites with Responsive Design

While I was working on my project, I realized that using only pixel values in tables and sidebars was a big mistake. This caused issues because if someone had a different screen resolution, my website would look distorted and unappealing. Could you please ...

Troubleshooting Google Tag Manager in Google Tag Assistant with the GTM script enclosed in an iframe on a separate domain

Recently, I have encountered a strange problem with the GTM's debugging feature. It was working perfectly fine before the interface got revamped. Previously, all I needed to do was enter the URL of the website and the debugger would successfully conn ...

Mobile responsiveness issues with Bootstrap 4 row

<!doctype html> <html lang="en"> <head> <!-- Required meta tags --> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <!-- Bootstrap CSS ...

Styling a clock with rounded corners using CSS: Border-radius trick

Attempting to craft a analog 12-hour clock using basic CSS properties, I initiated the process by forming a square div of the size 500px. Applying a border-radius of 250px resulted in an aesthetically pleasing circle design. Following this step, I incorpor ...

Error 400 - Bad Request in Spring MVC when trying to make an

I encountered a "400 : Bad Request" error when trying to trigger an Ajax request for the 'up-right-corner' login feature. The request URL appears to be correct in Firebug, but it consistently returns the "400 : Bad Request" response. It seems lik ...

Creating visible and invisible containers using CSS

I am facing an issue with the type switcher and containers marked by red rectangles. Each container has its own ID, and when one option is selected in the type switcher, all other containers should be invisible. For example, if the "Furniture" option is ch ...

Retrieving JSON data from Flickr's API collection

Currently in the process of constructing an uncomplicated photopage that is capable of displaying an endless amount of photos sourced from a Flickr set. The code I am working with looks like this: $(window).load(function() { var apiKey = 'XXX'; ...

Steps to trigger an alert when the entered quantity exceeds the current stock levels

After developing an IMS System, I encountered a problem where my stock is going into negative figures. To resolve this issue, my primary goal is to trigger an alert whenever the quantity entered by a user exceeds the available stock. For example, if a us ...

Scrolling the inner div with the main scrollbar before navigating through the rest of the page

In my hero div, I have a container div called right-col that contains two inner divs. These inner divs are sticky, creating the effect of cards sliding up when the container div is scrolled. This is the HTML structure: <!DOCTYPE html> <html lang= ...

Utilize local storage to dynamically update the background color of a div section

Struggling to create a functionality on my website where users can input a color, save it to localStorage, and have a specific div's background set to that color across all pages. The main content div (with the same ID) needs to display this color con ...

The jQuery function .val()/.text() is unable to retrieve information from a particular section of HTML

Implementing HandlebarsJS with PHP and Yii to generate a page. Here is a snippet of the html/handlebars code on the page {{#if embed_link}} <p class='f_hidden_p'> <a href='{{embed_link}}'> {{ ...

Tips for repairing a horizontal line at the bottom of the <TD> tag and placing text on top

I am working with an HTML table that contains a single TR tag and two TD tags. Each TD has two HR tags creating horizontal lines, along with some text. I am looking for a way to keep the text aligned at the top of the TD and the horizontal line at the bott ...

CSS Rules with Lower Specificity Will Take Precedence Over Rules with Higher Specific

Currently, I am enrolled in an online web development course and have been working on creating webpages to grasp the concepts of HTML and CSS. However, I encountered an issue where the font-family rules in my p and h3 elements were overriding the font-fami ...

Trouble with Click event not working in Electron and mouse cursor not changing when hovering over an HTML button?

I'm in the midst of a project that involves using the electron framework. Within my project, I have an HTML file, a CSS file, and a Javascript file. The issue at hand is that when I hover over a button, the expected hand pointer icon fails to appear d ...