Your JavaScript is fine, your problem is that the <span>
is collapsing to 0 width and 0 height, because there's nothing inside of it. Thus, it cannot be clicked, and only receives focus when tabbed onto, etc. You can change the display property to inline-block
or block
and set a width on the element to solve this problem.
var contenteditable_el = document.getElementById('contentEditable-name');
contenteditable_el.contentEditable='true';
// Try commenting these two lines out
contenteditable_el.style.display = "inline-block";
contenteditable_el.style.minWidth = "20px";
contenteditable_el.style.outline = "1px solid red";
<p>type here:
<span id="contentEditable-name">
</span>
</p>