Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the cons of using a contentEditable div rather than a textarea?

Would I be shooting myself in the foot by using a div with attribute contentEditable="true" as a text field rather than a textarea?

like image 357
Babiker Avatar asked Mar 12 '11 17:03

Babiker


People also ask

Should you use Contenteditable?

Would I be shooting myself in the foot by using a div with attribute contentEditable="true" as a text field rather than a textarea? If you want a text field, use a textarea, it's less likely to screw things up. Only use a contentEditable div when you need the ability to format the text. It would not work with forms.

What is a Contenteditable Div?

Definition and Usage. The contenteditable attribute specifies whether the content of an element is editable or not.

What is Contenteditable attribute used for?

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.

What is false about Contenteditable attribute?

contenteditable="false" Indicates that the element is not editable. contenteditable="inherit" Indicates that the element is editable if its immediate parent element is editable.


4 Answers

It would work fine, but it'd be a little bit more difficult than a form, simply because you're going to have to wire up your own logic to make the button's click event track down the correct div, get its content, and then perform the submission. The advantage of a textarea is that the browser takes care of all that for you.

like image 101
TML Avatar answered Oct 05 '22 19:10

TML


It's not the same thing. First semantically, the purpose of a textarea is to write/edit plain text whereas with contentEditable you can edit list for instance (see: htmldemo) Second the behavior is also different. For example, in chrome when you test the link below and that you delete all the content you loose the focus (the div element disappear) which is not the expected behavior, or if it is it's idiot.

like image 40
grandouassou Avatar answered Oct 05 '22 21:10

grandouassou


The Gmail's mail edit box is also a div with contenteditable="true". The major benefit is it has auto-adjust height as user input text/content. Also it supports rich text inside. You can mimic the Textarea by setting a max height if need.

On the other hand if you want auto height in Textarea, you might have to use js to bind some listener to the oninput hook.

like image 31
LeOn - Han Li Avatar answered Oct 05 '22 19:10

LeOn - Han Li


In divs with contenteditable="true" the content can be html formatted, e.g. text with different colors. See also https://stackoverflow.com/a/40898337/11769765.

like image 27
Friedrich Avatar answered Oct 05 '22 19:10

Friedrich