Maxlength for GWT TextArea
I am unable to set the maximum length for a GWT TextArea. Could someone help me achieve this in GWT?
TextArea t1 = new TextArea();
t1.setMaxLength(300); // T开发者_JS百科his method doesn't exist. How do I do this?
Gal's answer is right with just one correction :
t1.getElement().setAttribute("maxlength", "100");
The second parameter is a string. This worked for me.
You can set it as such:
t1.getElement().setAttribute("maxlength", "100");
Its cause maxLength is a html5 feature, so it would not work in older browsers. You have to doit by yourself. Just add a keyPresshandler and count the length of the text in the textarea and cut the text if its to long.
For the browsers which doesn't support HTML5, here is a workaround (an extension of gwt TextArea class with maxLength feature.)
http://www.karmicbee.com/gwt-textarea-with-max-character-limits/
精彩评论