Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make a div with contentEditable set show a blinking cursor on page load?

I would just like to have an editable div that acts like a textarea that is given focus on page load (i.e. the blinking cursor is visible and typing shows up in the div without having to select the div with the mouse). I've tried calling focus() on the editable div, but that doesn't work.

like image 563
user53937 Avatar asked Sep 20 '25 03:09

user53937


1 Answers

I'm not sure it's possible to control the cursor, but you can simply focus the element:

function initPage() {
    var elEd = document.getElementById('editor');
    elEd.contentEditable=true;
    elEd.focus();
}

In Chrome, if your element with ID editor has any content then the whole content will be selected. In Firefox you don't see a cursor, but if you type after loading the page it will appear in the element. Simple example here.

like image 132
robertc Avatar answered Sep 22 '25 19:09

robertc