Simple jQuery scripts
The post contains simple but useful implications of jQuery.
Set temporary blink effect on element:
$('#ok_message').fadeOut(400).fadeIn(400).fadeOut(400).fadeIn(400);
Reset web form:
$('#myform')[0].reset();
Detect the scroll direction:
var lastScrollTop = 0;
$(document).scroll(function(event){
var st = $(this).scrollTop();
if (st > lastScrollTop){
console.log('scrolling down...');
} else {
console.log('scrolling up...');
}
lastScrollTop = st;
});
Get the length of textarea value while considering the new lines as two symbols (bytes):
$('#example').on("paste keyup cut", function(e) {
var value = $(this).val();
value_length = value.replace(/\r(?!\n)|\n(?!\r)/g, "\r\n").length;
if(e.type == 'paste') {
value_length += e.originalEvent.clipboardData.getData('text').replace(/\r(?!\n)|\n(?!\r)/g, "\r\n").length;
} else if(e.type == 'cut') {
value_length -= window.getSelection().toString().replace(/\r(?!\n)|\n(?!\r)/g, "\r\n").length;
}
console.log(value_length);
});