Character countdown like on twitter

后端 未结 9 907
栀梦
栀梦 2020-12-02 06:54

How can I make a \"remaining characters\" countdown like the one on Twitter with jQuery? And also limit the input to a textarea.

相关标签:
9条回答
  • 2020-12-02 07:28

    It's a suicide to use jQuery, Mootools or similar for such simple task ... unless you are already using them for something else.

    Simple javascript function:

    function limitTextCount(limitField_id, limitCount_id, limitNum)
    {
        var limitField = document.getElementById(limitField_id);
        var limitCount = document.getElementById(limitCount_id);
        var fieldLEN = limitField.value.length;
    
        if (fieldLEN > limitNum)
        {
            limitField.value = limitField.value.substring(0, limitNum);
        }
        else
        {
            limitCount.innerHTML = (limitNum - fieldLEN) + ' charachter(s) to go.';
        }
    }
    

    The first parameter is id of the input/textarea. The second - id of the div to display characters left. The third is the limit itself.

    And simple HTML:

    <input type="text" value="" name="title" id="title" maxlength="100" onkeyup="limitTextCount('title', 'divcount', 100);" onkeydown="limitTextCount('title', 'divcount', 100);">
    <br>
    <div id="divcount">100 charachter(s) to go..</div>
    
    0 讨论(0)
  • 2020-12-02 07:29

    use jquery

    save this as jquery.textlimiter.js

    (function($) {
        $.fn.extend( {
            limiter: function(limit, elem) {
                $(this).on("keyup focus", function() {
                    setCount(this, elem);
                });
                function setCount(src, elem) {
                    var chars = src.value.length;
                    if (chars > limit) {
                        src.value = src.value.substr(0, limit);
                        chars = limit;
                    }
                    elem.html( limit - chars );
                }
                setCount($(this)[0], elem);
            }
        });
    })(jQuery);
    

    usage

    <textarea id="text" maxlength="100" cols="50" rows="5" placeholder="Enter Text"></textarea>
        <div id="chars">100</div>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
    <script src="path/to/jquery.textlimiter.js"></script>
    <script>
    $(document).ready( function() {
        var elem = $("#chars");
        $("#text").limiter(100, elem);
    });
    </script>
    
    0 讨论(0)
  • 2020-12-02 07:32

    I added a simple pluralize function, cus nobody likes bad grammar.

    function pluralize(count, word){
      if (count == 1 || count == -1){
        return String(count) + ' ' + word;
      }else{
        return String(count) + ' ' + word + 's';
      }
    }
    
    function updateCountdown() {
        // 140 is the max message length
        var remaining = 140 - jQuery('#micropost_content').val().length;
        jQuery('.countdown').text(pluralize(remaining,'character') + ' remaining');
    }
    
    ...
    
    0 讨论(0)
提交回复
热议问题