Created
April 8, 2011 04:54
-
-
Save iancanderson/909312 to your computer and use it in GitHub Desktop.
new jquery plugin in progress to support www.pilvinotes.com
This file contains hidden or 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
(function( $ ){ | |
$.fn.pollForChanges = function( interval, changedCallback ) { | |
// this maps element ids to corresponding values | |
var _mapIdsToValues = {}; | |
// skip if not a textarea or an text input (for now) | |
var inputs = this.filter("textarea,input[type='text']"); | |
setInterval( function() { | |
var aryIdsChanged = []; | |
// figure out which elements' values changed | |
inputs.each(function() { | |
var elementId = this.id; | |
// get the new value of this element | |
var newValue = $("#" + elementId).val(); | |
// add id to array if value changed | |
if (newValue !== _mapIdsToValues[elementId]) { | |
aryIdsChanged.push(elementId); | |
} | |
// store the new value | |
_mapIdsToValues[elementId] = newValue; | |
}); | |
changedCallback(aryIdsChanged); | |
}, interval); | |
}; | |
})( jQuery ); |
This file contains hidden or 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
<html> | |
<head> | |
<title>test</title> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> | |
<script src="jquery.pollForChanges.js"></script> | |
</head> | |
<body> | |
<input type="text" class="pollForChanges" id="tagList"></input> | |
<textarea class="pollForChanges" id="noteContent" rows="20" cols="60"></textarea> | |
<p id="status"></p> | |
<script type="text/javascript"> | |
$(function() { | |
f1 = function( aryIdsChanged ) { | |
$.each(aryIdsChanged, function(index, value) { | |
$("#status").prepend(value + " <strong>changed!</strong><br/>"); | |
}); | |
} | |
$(".pollForChanges").pollForChanges(1000, f1); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment