How to Add Maxlength On TextArea Using JQuery
here is the pretty good and easy to understood code for making textarea working with maxlength property.
JQuery Code :
$(document).ready( function () { maxLength = $("textarea#comment").attr("maxlength"); $("textarea#comment").after("" + maxLength + " remaining"); $("textarea#comment").bind("keyup change", function(){checkMaxLength(this.id, maxLength); } ) }); function checkMaxLength(textareaID, maxLength){ currentLengthInTextarea = $("#"+textareaID).val().length; $(remainingLengthTempId).text(parseInt(maxLength) - parseInt(currentLengthInTextarea)); if (currentLengthInTextarea > (maxLength)) { // Trim the field current length over the maxlength. $("textarea#comment").val($("textarea#comment").val().slice(0, maxLength)); $(remainingLengthTempId).text(0); } }
HTML Code :
<body> <h1>TextArea maxlength with jQuery</h1> <textarea id="comment" maxlength="10" rows="10" cols="60" ></textarea> </body>
Working Demo
Any more Questions ? let me know with comments.
for projects write me at bhavinrana07@gmail.com
A universal message I suppose, not giving up is the formula for success I think. Some things take longer than others to accomplish, so people must understand that they should have their eyes on the goal, and that should keep them motivated to see it out til the end.
ReplyDeleteReactjs online training