public
Last active

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.

  • Download Gist
changepolling.joelpurra.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
/*!
* @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));

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.