Skip to content

@joelpurra /changepolling.joelpurra.js
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
A jQuery plugin to check if the value has changed in an input/select/textarea without relying on the jQuery .change() event. This is perfomed using polling at a configurable interval, which will then trigger the .change() event.
/*!
* @license ChangePolling
* Copyright © 2012 Joel Purra <http://joelpurra.se/>
* Released under MIT, BSD and GPL license. Comply with at least one.
*
* A jQuery plugin to check if the value has changed in an input/select/textarea
* without relying on the jQuery .change() event. This is perfomed using polling
* at a configurable interval, which will then trigger the .change() event.
*/
// https://gist.github.com/2944926
//
// USAGE
// $("#my-input-to-poll").changePolling();
//
// DEFAULT OPTIONS
// $("#my-input-to-poll").changePolling({
// interval: 1000,
// autoStart: true
// });
//
/*jslint vars: true, white: true, browser: true*/
/*global jQuery, window*/
(function($, window) {
"use strict"; // jshint ;_;
var tag = "changePolling",
ChangePolling = function(element, options) {
this.$element = $(element);
this.options = $.extend(true, {}, $.fn.changePolling.defaults, options);
this.options.previousValue = this.$element.val();
this.stop();
if (this.options.autoStart === true) {
this.start();
}
};
ChangePolling.prototype = {
constructor: ChangePolling
,
start: function() {
this.options.intervalId = window.setInterval($.proxy(this.checkForChange, this), this.options.interval);
}
,
stop: function() {
window.clearInterval(this.options.intervalId);
this.options.intervalId = null;
}
,
checkForChange: function() {
var currentValue = this.$element.val();
if (this.options.previousValue !== currentValue) {
// Trigger change event - "this is where the magic happens"
this.$element.change();
}
this.options.previousValue = currentValue;
}
};
$.fn.extend({
changePolling: function(option) {
return this.each(function() {
var $this = $(this),
data = $this.data(tag),
options = typeof option === "object" && option;
if (!data) {
$this.data(tag, (data = new ChangePolling(this, options)));
}
if (typeof option === "string") {
data[option]();
}
});
}
});
$.fn.changePolling.defaults = {
interval: 1000,
autoStart: true
};
$.fn.changePolling.Constructor = ChangePolling;
}(jQuery, window));
@nshathish

hi
how do use this and set a bool flag when a textbox content is changed ?
thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.