• Saturday, March 10, 2012
  • Instant Character Count like Twitter with jQuery



    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> 

    HTML Code:

    Here some html code.

    <textarea id="txtComment" name="txtComment"></textarea>
    <span class="counter"></span>

    Share this post

    Kathirason Asokan
    About the Author
    I'm , I love writing code in PHP, and spending time with friends. My hobbies are blogging, surfing internet, juggling etc.,.

    0 comments:

    Post a Comment

     
    Copyright (c) 2011 - 2021 techispeaks.com. All rights reserved the content is copyrighted to Kathirason Asokan
    Creative Commons License