Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

contenteditable div not actually editable in webkit

I have a div with the contenteditable attribute set to to true, however, it does not show a blinking cursor or update when I type text into it. I've added event listeners for focus, keydown, and keypress to see if the div is receiving them, AND IT IS!

How could it be that all the appropriate events are actually firing yet the content of the div is not appropriately updating itself? I'm not doing anything funky like preventing the default behavior of the events.

Also, I've gotten contenteditable working just fine several times before on different projects, so I'm fairly certain it's some bug being caused by the structure of this particular page's HTML. Again, the issue only occurs in Chrome and Safari; Firefox and IE8 are fine.

like image 511
mateolargo Avatar asked Nov 22 '10 23:11

mateolargo


People also ask

How do I make a div content 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.

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.

How do I turn off Contenteditable div?

Just set contentEditable="false" . See this answer.

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.


2 Answers

Okay, I figured out my problem. I had the css property '-webkit-user-select' set to none on an element way up the hierarchy of the div in question. Thus it was preventing the cursor from ever being positioned within my contentEditable div.

Interestingly, the corresponding '-moz-user-select' (which I also have set to none up the hierarchy) does not affect Firefox in the same way.

like image 143
mateolargo Avatar answered Sep 28 '22 09:09

mateolargo


I'd like to share this code with you. It might help you out! You disable the user-select and callout for the whole site, and then override the contenteditable fields to allow user-select.

html,body{
    -webkit-user-select: none;
    -webkit-touch-callout: none;
}

*[contenteditable] {
    -webkit-user-select: auto !important;
}
like image 42
Federico Avatar answered Oct 02 '22 09:10

Federico