What is the best way to emulate an HTML input “maxlength” attribute on an HTML textarea?

后端 未结 8 1349
执笔经年
执笔经年 2020-11-28 11:54

An HTML text input has an attribute called \"maxlength\", implemented by browsers, which if set blocks user input after a certain number of characters.

An HTML texta

8条回答
  •  余生分开走
    2020-11-28 12:35

    I think the following is as "clean" as it gets. Make an event handler in Javascript that takes the keydown event and the textarea field as input. Check the length of the text in the field in this handler and return false if the maxlength is reached. (Of course make any css style switching before returning). Return true otherwise. The textarea should look like this.

    
    

提交回复
热议问题