Last active
August 7, 2023 14:33
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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