Jquery plug-in achieves automatic height of multi-line text boxes [textarea]

Source: Internet
Author: User

Jquery plug-in achieves automatic height of multi-line text boxes [textarea]

This article mainly introduces the jquery plug-in to implement the automatic height of multi-line text boxes [textarea]. For more information, see

 

 

Functions:

1. When textarea is changed, the height of a row is automatically increased.
2/When textarea deletes a row, the high dependency of the row is automatically reduced: jquery. xxx. js requires similar functions, but it is inconvenient to import other files to the plug-in.

Textarea jquery plugin

 

The Code is as follows:


<Div class = "form-group">
<Label class = "col-sm-3 control-label no-padding-right" for = "form-field-5"> content </label>
<Div class = "col-sm-9">
<Textarea class = "col-sm-8" id = "form-field-5" placeholder = "Enter the content..."> </textarea>
</Div>
</Div>

 

 

The Code is as follows:


JQuery. extend ({
TextareaAutosize_dc: function (){
$ ("Textarea"). on ("keyup", function (e ){
Var currentEnterCount = $ (this). val (). split ("\ n"). length;
Var lineHeight = number(((this).css ("line-height"). replace ("px ",""));
Var enterCount = $ (this). attr ("enterCount ");
If (currentEnterCount <enterCount & enterCount! = Undefined ){
// Remove the fixed Row Height from each row
$ (This). height ($ (this). height ()-lineHeight );
} Else if (currentEnterCount> enterCount ){
// Add fixed Row Height to each row
$ (This). height ($ (this). height () + lineHeight );
$ (This). attr ("enterCount", currentEnterCount );
}
// Record the current Row Height
$ (This). attr ("enterCount", currentEnterCount );
});
}
});
// Automatic call height
$. TextareaAutosize_dc ();

 

The above is all the content of this article. I hope you will like it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.