Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a content editable div behave like a text area?

I have built an editor that converts markdown to html. Right now I have to use jquery autosize plugin to resize the text area as it grows.

If I use a content-editable div I can bypass it. But the problem with content editable div is that it does not preserve new lines. It inserts a new div every time return key is pressed. This breaks the rendering of markdown to html for my application.

Is there any way I can make a content editable div behave exactly like text area?

like image 363
Akshat Jiwan Sharma Avatar asked May 23 '13 13:05

Akshat Jiwan Sharma


People also ask

How do I make a div text editable?

Answer: Use the HTML5 contenteditable Attribute You can set the HTML5 contenteditable attribute with the value true (i.e. contentEditable="true" ) to make an element editable in HTML, such as <div> or <p> element.

How do you make an editable area in HTML?

Type a <div> section for the editable region. The <div> section must contain a unique ID that contains the text _Container. If you want to apply styles to the editable region, enclose it in a <div> section containing the style information.

How do I separate text in a div?

Add the style word-wrap: break-word; to the div. It is a CSS3 property that works in ALL browsers (even IE5!)

How do I make a text input non editable for a part of it?

The readonly attribute makes a form control non-editable (or “read only”). A read-only field can't be modified, but, unlike disabled , you can tab into it, highlight it, and copy its contents.


2 Answers

After searching for an answer and not finding anything that worked completely I wrote my own jQuery plugin.

https://github.com/UziTech/jquery.toTextarea.js

I used white-space: pre-wrap; and inserted '\n' on enter. That way I can use $("div").text() to get the text and not worry about removing tags and formatting <br/>'s

DEMO:

http://jsfiddle.net/UziTech/4msdgjox/

like image 167
Tony Brix Avatar answered Oct 21 '22 22:10

Tony Brix


Edit

After the @Mr_Green comment above, you should have a look at Make a <br> instead of <div></div> by pressing Enter on a contenteditable

The JS code to make it right is :

$(function(){

  $("#editable")

    // make sure br is always the lastChild of contenteditable
    .live("keyup mouseup", function(){
      if (!this.lastChild || this.lastChild.nodeName.toLowerCase() != "br") {
        this.appendChild(document.createChild("br"));
      }
    })

    // use br instead of div div
    .live("keypress", function(e){
      if (e.which == 13) {
        if (window.getSelection) {
          var selection = window.getSelection(),
              range = selection.getRangeAt(0),
              br = document.createElement("br");
          range.deleteContents();
          range.insertNode(br);
          range.setStartAfter(br);
          range.setEndAfter(br);
          range.collapse(false);
          selection.removeAllRanges();
          selection.addRange(range);
          return false;
        }
      }
    });

})

;


You can intercept the Enter key press and replace it with a <br> with Javascript :

$(function(){
    
      $("#editable").keypress(function(e) {
        if (e.which == 13) {
            e.preventDefault();
            
             if (document.selection) {
                document.selection.createRange().pasteHTML("<br/>");
             } else {
                $(this).append("<br/>");
             }
        }
    });
});
like image 20
Xavier Avatar answered Oct 21 '22 23:10

Xavier