In this post I would like to explain how to do character count in jQuery. I have did the same thing in many of my projects to enrich my web applications. This will be very help full for you in comment section to restruct the user to limited character.
Javascript Code:
$('#txtComment').each(function(){ - txtComment is the textbox ID. $(this).val().length getting the length of the value. $('.counter') - counter is the span ID which display the char count number.
<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js' type='text/javascript'></script>
<script type='text/javascript'>
$(document).ready(function(){
$('#txtComment').each(function(){
var length = $(this).val().length;
$('.counter').html( 'chars left ' + (1000 - length));
$(this).keyup(function(){
var new_length = $(this).val().length;
$('.counter').html( 'chars left ' + (1000 - new_length) );
});
});
});
</script>
<script type='text/javascript'>
$(document).ready(function(){
$('#txtComment').each(function(){
var length = $(this).val().length;
$('.counter').html( 'chars left ' + (1000 - length));
$(this).keyup(function(){
var new_length = $(this).val().length;
$('.counter').html( 'chars left ' + (1000 - new_length) );
});
});
});
</script>
HTML Code:
Here some html code.
<textarea id="txtComment" name="txtComment"></textarea>
<span class="counter"></span>
<span class="counter"></span>
0 comments:
Post a Comment