Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Edit cursor not displayed on Chrome in contenteditable

Tags:

When you open this page (see Live demo) with Chrome :

<span id="myspan" contenteditable=true></span> 

CSS :

#myspan { border: 0; outline: 0;} 

JS :

$(myspan).focus(); 

the contenteditable span has focus (you can start to write things and you will see that it already had focus), but we don't see the "I" edit cursor.

How to make that this cursor is displayed ? (Remark : outline:0 is needed, as well as the fact that the span is empty even with no white space).

Note : With Firefox, the cursor is displayed.

like image 304
Basj Avatar asked Sep 17 '14 18:09

Basj


People also ask

How do I turn off Contenteditable div?

set contenteditable to false and it should work !! that simple. use contenteditable attribute for div to make it editable or not and use readonly attr for form input elements. Save this answer.

How do I stop Enter key in Contenteditable?

To prevent contenteditable element from adding div on pressing enter with Chrome and JavaScript, we can listen for the keydown event on the contenteditable element and prevent the default behavior when Enter is pressed. to add a contenteditable div. document. addEventListener("keydown", (event) => { if (event.

How do I save changes in Contenteditable?

After users have made some edits, they can click on the 'Save Changes' button and the changes made can be saved permanently. If the title is editable, we set the contentEditable property on each of the editable elements to false.

How do I make my page Contenteditable?

All you have to do is set the contenteditable attribute on nearly any HTML element to make it editable. Here's a simple example which creates a <div> element whose contents the user can edit.


2 Answers

The problem is that spans are inline elements. Just add display:block; to your CSS and it will fix the problem.

$(myspan).focus();
#myspan {      border: 0;      outline: 0;      display: block;  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>  <span id="myspan" contenteditable=true></span>
like image 84
imtheman Avatar answered Sep 18 '22 14:09

imtheman


I added padding to the left and the cursor appears.

#myspan {     border: 0;      outline: 0;      min-width: 100px;      height: 30px;      padding-left: 1px; } 

Demo in jsFiddle

like image 37
Luke Avatar answered Sep 16 '22 14:09

Luke