Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a div autogrow where contenteditable=true

How (using jquery or javascript) can I make <div contenteditable></div> autogrow when I type more text in it?

like image 608
Joe Avatar asked Apr 06 '13 22:04

Joe


People also ask

How do you change Contenteditable in Javascript?

You can add the contenteditable="true" HTML attribute to the element (a <div> for example) that you want to be editable. If you're anticipating a user to only update a word or two within a paragraph, then you could make a <p> itself editable.

What does Contenteditable inherit mean?

contenteditable="inherit" Indicates that the element is editable if its immediate parent element is editable. This is the default value. When you add contenteditable to an element, the browser will make that element editable.

How does Contenteditable work?

The contenteditable global attribute is an enumerated attribute indicating if the element should be editable by the user. If so, the browser modifies its widget to allow editing.

Is Contenteditable angular?

Angular does not have an accessor for contenteditable , so if you want to use it with forms you will have to write one.


1 Answers

Avoid giving a height style property, and the auto-resize should happen automatically. If you must give a height, use the min-height property instead:

<div contenteditable="true" 
     style="background: #ddd; border: 3px solid #ccc; min-height: 60px">
</div>

See it in action: FIDDLE

like image 113
Sagish Avatar answered Oct 04 '22 19:10

Sagish