Skip to content

Instantly share code, notes, and snippets.

@ckentq
Created December 2, 2015 01:36
Show Gist options
  • Save ckentq/504afaffba966e27f42c to your computer and use it in GitHub Desktop.
Save ckentq/504afaffba966e27f42c to your computer and use it in GitHub Desktop.
//count words
$(".count").each(function() {
var minWords = 0;
var maxWords = <?php echo $counts; ?>;
var _flag = false;
$(this).after('<div class="wordCount"><strong>0</strong> Words</div>');
//count words in begining
$(this).siblings('.wordCount').children('strong').text(jQuery.trim($(this).val()).split(' ').length);
//count words when change
$(this).bind('keyup click blur focus change paste', function() {
var numWords = jQuery.trim($(this).val()).split(' ').length;
if($(this).val() === '') {
numWords = 0;
}
$(this).siblings('.wordCount').children('strong').text(numWords);
if(numWords < minWords || (numWords > maxWords && maxWords != 0)) {
$(this).siblings('.wordCount').addClass('error');
_flag = false;
} else {
$(this).siblings('.wordCount').removeClass('error');
_flag = true;
}
});
//save comment when leave
var _this = $(this);
//$(this).parent().children('.save_comment').bind('click', function(){
$(this).bind('focusout', function(){
if(_flag){
$('#wait_ajax').show();
var _comment = _this.attr('value');
var _id = _this.attr('id');
$.post("<?php echo site_url();?>/cms/<?php echo $page_root; ?>/save_esgr_comment", { id: _id, comment: _comment, esgr_id:<?php echo $result_esgr->id;?>},
function(data){
$('#wait_ajax').hide();
$('#messeage_alert').html(data['massage']).show(10).delay(1000).hide(10);
}, "json");
}else{
window.alert('Please reduce comment to <?php echo $counts; ?> words!');
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment