Skip to content

Instantly share code, notes, and snippets.

@joelpurra
Created June 17, 2012 15:53
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joelpurra/2944926 to your computer and use it in GitHub Desktop.
Save joelpurra/2944926 to your computer and use it in GitHub Desktop.
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
Copy link

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