Giter Club home page Giter Club logo

auto-form-polyfill's Introduction

auto-form-polyfill

Triggers change and input events when browsers autocomplete or autofill form inputs. Requires jQuery.

##The problem

When a browser autocompletes or autofills a form field, it can’t be guaranteed that change or input events will fire. This is particularly troublesome for frameworks like Angular that rely on the input event to bind elements to models. Read this article from 2010 by Alessandro Vernet for more details. The problem hasn’t changed much in the four years since.

##The solution

This polyfill fires input and change events on the browser’s behalf. It uses polling to infer autocomplete and click events to infer autofill. The click event listener is bound to the window with event delegation to ensure that dynamic elements are included.

Polling is crude. Until there’s a standard, all solutions will be crude. Expect to see false positives.

##What’s the difference between autocomplete and autofill?

We’re using Chrome’s terminology here. “Autocomplete” means the browser supplies values for a whole form when it’s loaded. “Autofill” means the browser suggests values for individual fields on focus. It may also fill other fields in the same form.

##API

// Not required - these are the defaults
autoFormPolyfill.configure({

	// Defensively assumes that all inputs have been autocompleted before .start() can be called
	fireChangesOnStart: true,

	// The more fields there are to poll, the bigger this number should be
	pollingIntervalMs: 100,

	// The change event refers specifically to user input - in the case of autofill/autocomplete, it's open to interpretation
	eventsToTrigger: ['input', 'change']

});

// Starts the polling and binds the click handler
autoFormPolyfill.start();

// Returns true
autoFormPolyfill.isRunning();

// Stops the polling and unbinds the click handler
autoFormPolyfill.stop();

// Now returns false
autoFormPolyfill.isRunning();

##Use with Angular

function YourController ($scope) {

	autoFormPolyfill.start();

	// For apps with routes / deep-linking
	$scope.$on('$destroy', function () {
		autoFormPolyfill.stop();
	});

}

auto-form-polyfill's People

Contributors

adamshaylor avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.