Wednesday, April 3, 2013

How to Add Maxlength On TextArea Using JQuery

How to Add Maxlength On TextArea Using JQuery

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



Reactions:

0 comments:

Post a Comment

Any Questions or Suggestions ?

About

Professional & Experienced Freelance Developer From India, Technologist, Software Engineer, internet marketer and Open Sources Developer with experience in Finance, Telecoms and the Media. Contact Me for freelancing projects.

Enter your email address:

Delivered by FeedBurner

PHP Freelancer India - Google+