Skip to content

Instantly share code, notes, and snippets.

@kadimi
Last active August 7, 2023 14:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kadimi/0136cf0d25cea3d58e02 to your computer and use it in GitHub Desktop.
Save kadimi/0136cf0d25cea3d58e02 to your computer and use it in GitHub Desktop.
Sometimes I need the "change" event to be triggered even when elements that are not changed directly by the user, this works good so far.
var t = 200; // The interval at which new change ares detected
jQuery( document ).ready( function() {
// Detect remote change
$( 'input, textarea' ).each( function() {
var $this = $( this );
$this.data( 'value', $this.val() );
setInterval( function() {
if( $this.val() !== $this.data( 'value' ) ) {
// Do not trigger if element does not have focus
if( $this.not( ':focus' ) ) {
$this.change();
}
$this.data( 'value', $this.val() );
}
}, t );
} );
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment