Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a div to a input text form?

There is some drawbacks using textarea and input-text as input of text forms. textarea has a little annoying triangle in right-lower corner and input-text is a single-line input.

I try to have a input of text like the facebook update input form. The input auto resize after linebreaks. And the element or tag used was <div>. I said "used" because, after they redesigned Facebook, I can't figure-out which tag is used now. There is CSS property that enables the user to edit the text in a div element. I actually copied the CSS property, but now I lost it. Can someone tell me which CSS property it was? I have a weak memory that it began with the -webkit prefix though

like image 688
einstein Avatar asked Jan 02 '11 22:01

einstein


People also ask

Can you put a div in a input?

You can't place any element inside an input. An input does not contain any HTML.

How do I make my div act like an input?

Using an enumerated HTML attribute called contentEditable, you can turn an HTML div element into an editable textbox or textarea.

Can I put text directly in a div?

Yes, you can directly add content text into a div tag, although using p tags would be preferable in most circumstances.

Can I use div instead of form?

It is valid but violates the principles of unobtrusive JavaScript, introduces accessibility issues and removes useful APIs.


2 Answers

If you use html5 you can use:

<div id="divThatYouCanWriteStuffIn" contenteditable>
<!-- you can write in here -->
</div>

If you couple this with the css:

#divThatYouCanWriteStuffIn {
    min-height: 4em; /* it should resize as required from this minimum height */
}

To get rid of the 'annoying little triangle' in textareas:

textarea {
  resize: none;
}

JS Fiddle demo of both ideas.

like image 136
David Thomas Avatar answered Sep 27 '22 17:09

David Thomas


I know you can do this in javascript by doing getElementByID('mydiv').contentEditable='true';, but I do not know how this would be done in CSS

like image 27
Colum Avatar answered Sep 27 '22 17:09

Colum