How to prevent user to enter text in textarea after reaching max character limit

后端 未结 9 1435
盖世英雄少女心
盖世英雄少女心 2020-11-29 01:52

I want to prevent user to enter text in textarea once it reaches a max character limit. What was happening that when i reached to max limit then my text-area scroll-bar move

相关标签:
9条回答
  • 2020-11-29 02:45

    Keep it simple

    var max = 50;
    $("#textarea").keyup(function(e){
    $("#count").text("Characters left: " + (max - $(this).val().length));
    });
    

    and add this in your html

    <textarea id="textarea" maxlength="50"></textarea>
    <div id="count"></div>
    

    view example

    0 讨论(0)
  • 2020-11-29 02:47

    You could use this plugin instead of trying to write your own. I've found that it works pretty well.

    0 讨论(0)
  • 2020-11-29 02:49

    You can keep your event as they are , and just use this library

    Examples

    // applying a click event to one element
    
    Touche(document.querySelector('#myButton')).on('click', handleClick);
    
    // or to multiple at once
    
    Touche(document.querySelectorAll('.myButtons')).on('click', handleClicks);
    
    // or with jQuery
    
    $('.myButtons').on('click', handleClicks);
    
    0 讨论(0)
提交回复
热议问题