views:

1245

answers:

5

Hi,

I need to make a div layer so that when you click on it you will have your cursor there blinking and you can insert/delete text just like <input type="text"> does, except that I do not want to use it as it is slightly too limited to my case.

I can definitely use JavaScript.

+6  A: 

DIV element has (other elements as well) contentEditable property that you can set in Javascript to true.

getElementById('YourDiv').contentEditable = true;
Robert Koritnik
Can be done in markup as well: <div contentEditable="true">Editable content</div>
Fredrik Mörk
You cannot do that on an element, but only on the whole document (https://developer.mozilla.org/en/Rich-Text_Editing_in_Mozilla)
Fabien Ménager
Ok, I thought it was only possible on the document, sorry !
Fabien Ménager
Note that 'contentEditable' is not in the HTML4.01 specification, or in the DOM2 HTML specification (1.0). It is something added by browsers.
Colin Fine
It does, however, appear in the HTML5 **draft**: http://www.w3.org/TR/html5/editing.html#contenteditable
David Dorward
That is kind of cool, but I really just need a working JavaScript/CSS solution that allows me to add text into DIV's InnerHTML.
rFactor
A: 

I suggest you to use a textarea, and if that's not enough, use a WYSIWYG editor like tinyMCE or FCKeditor.

Fabien Ménager
+2  A: 

You can make the div editable by setting its contentEditable attribute / property to true. However, for anything that is slightly more powerful or flexible then very basic editing, you might want to look at existing solutions such as:

Daan
A: 

contenteditable attribute could be used for this purpose. Following code line has been tested in IE7 and Firefox 3.0.10. One part, I have noticed that this attribute should be used in lower case only; else it wont work in Firefox.

*<div id="Div_ID" contenteditable="true" tabindex="0">Enter text here</div>*

Green Techy
A: 

as I understand your problem, you can resove it by adding textarea into your div. It is very simply to make this textarea autosize to occupy whole div area and looks like this div.

As for contentEditable, I have seen some browsers, supported this feature for div-element and does not. Anyway, you can use iframe in your div. It's document-element can have contentEditable.

Sergey Kovalenko