Stack Overflow Detect Text Area Change
So, this shows what can be called via events and an interval to check for the trimmed length of the contents.
by Brad Chesney
HTML
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<div>
<textarea id="textarea" cols="45" rows="10"></textarea>
</div>
<div id="output"></div>
<div id="action"></div>
CSS
div {
display: block;
}
span {
padding: 3px;
}
JavaScript
var updateMessage = function() {
if ($.trim($('#textarea').val()).length < 1) {
$('#output').html('Someway your box is being reported as empty... sadness.');
} else {
$('#output').html('Your users managed to put something in the box!');
//No guarantee it isn't mindless gibberish, sorry.
}
},
init = function() {
$('#textarea').change(function() {
updateMessage();
$('#action').append("<span style=\"color:#f00;\">Change.</span>");
});
$('#textarea').blur(function() {
updateMessage();
$('#action').append("<span style=\"color:#600;\">Blur.</span>");
});
$('#textarea').keydown(function() {
updateMessage();
$('#action').append("<span style=\"color:#050;\">Key down.</span>");
});
$('#textarea').keyup(function() {
updateMessage();
$('#action').append("<span style=\"color:#0c0;\">Key up.</span>");
});
$('#textarea').mouseup(function() {
updateMessage();
$('#action').append("<span style=\"color:#cc0;\">Mouse up.</span>");
});
$('#textarea').mousedown(function() {
updateMessage();
$('#action').append("<span style=\"color:#550;\">Mouse down.</span>");
});
$('#textarea').click(function() {
updateMessage();
$('#action').append("<span style=\"color:#00f;\">Mouse click.</span>");
});
$('#textarea').mouseleave(function() {
updateMessage();
$('#action').append("<span style=\"color:#066;\">Mouse leave.</span>");
//-- a small textarea will potentially trigger before paste is clicked in the context menu if it the 'paste' menu item is outside of the element border.
});
setInterval(function() {
updateMessage();
$('#action').append("<span style=\"color:#555;\">Interval.</span>");
}, 10000);
};
$(document).ready(function() {
init();
});