8

I want to focus the contenteditable div, so that I can automatically start typing when it is inserted into the DOM

I have tried $('.content').focus(), but this does not work

Example html:

<div class="content" contenteditable="true">sample input</div>
marsh
  • 1,431
  • 1
  • 13
  • 19
Tarlen
  • 3,657
  • 8
  • 30
  • 54
  • possible duplicate of [Set cursor position on contentEditable
    ](http://stackoverflow.com/questions/1181700/set-cursor-position-on-contenteditable-div)
    – marsh May 10 '15 at 14:22
  • http://stackoverflow.com/a/14701053/2025923 Possible Duplicate – Tushar May 10 '15 at 14:22
  • possible duplicate of [Set focus on div contenteditable element](http://stackoverflow.com/questions/2388164/set-focus-on-div-contenteditable-element) – WiredPrairie May 10 '15 at 14:46

1 Answers1

3

I think you probably just need to wrap your code in a window.load function:

$(window).on("load",function(){
  $('.content').focus();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="content" contenteditable="true">sample input</div>
(fiddle: http://jsfiddle.net/hxeqm541/)

What this does is, it waits until the page is fully loaded, before it executes the code inside the window.load.
If you don't do this, the code to focus on the div will be executed before the div even exists on the page, which is impossible. By the time the div finally does exist on the page, the code has already run, so nothing will happen anymore.


Side notes:

  • Because you reference the div by its class ($('.content')), if you have more than one of these divs with the same class-name, only the last one will receive focus. See example.
  • You DO NOT NEED to wrap every individual code block in an window.load.
    Usually, you can wrap your ENTIRE JavaScript code in the window.load. There might be a few exceptions to that rule, read this and this to fully understand the workings of the window.load mechanism so you'll know exactly how to use it.
    (Notice in the second link that .load can also be used on other elements.)
myfunkyside
  • 3,890
  • 1
  • 17
  • 32
  • But I am inserting the contenteditabke div dynamically in response to certain user events like clicking and pressing a specific key. This means that the div is not present in the DOM when the page finishes it initial load – Tarlen May 11 '15 at 06:26
  • Then why didn't you specify that in your question? If you describe a general problem, you get a general solution. If you need help with a specific problem, we're gonna need the specifics... – myfunkyside May 11 '15 at 06:39
  • 1
    "automatically start typing when it is inserted into the DOM" – Tarlen May 11 '15 at 06:41
  • That is open for interpretation.. Now that I know it's DYNAMICALLY inserted, I'm inclined to read it like that, before I knew that, I wasn't. (I put "dynamically" in caps there for you in case you didn't understand yet that that would have been the key word that would have made everybody understand.. yes, that was sarcastic. I'm not trying to bust your balls, I'm trying to make a point: when you ask people to spend their free time working on your problem, you better damn well make sure you explain it to them as clear as you can, so there is no chance on misinterpretation (meaning wasted time). – myfunkyside May 11 '15 at 06:49
  • Directly under that ambiguous sentence, your only provided code of the content editable div in question, is the HTML code (`
    sample input
    `). Not the JS code that shows us how you insert the div dynamically, no function, not one line of js-code. Only ready-on-load HTML, which I'm willing to bet makes almost everyone here assume that that is how that div is represented in your script.
    – myfunkyside May 11 '15 at 06:54
  • it is taking the focus to the 0th index. Is it possible to birng the focus to where is was left – Vikas Bansal May 30 '17 at 08:38
  • @VikasBansal could you elaborate on your exact situation? (In general terms, it sounds like you would have to store some kind of reference to the element which had focus, so that you can put focus back on that element. But it's hard to give more advice without knowing your exact situation.) – myfunkyside May 30 '17 at 22:46
  • Many thanks for your reply @myfunkyside and you are right that I want to store some kind of reference but not to just an element but to the point some where in a text. Let me explain: suppose there is a `contenteditable` `div` and there are some `p` having some text like `the myfunckyside is awesome` and I want to take the cursor next to `is` (where I had left it earlier ) – Vikas Bansal May 31 '17 at 06:39
  • @VikasBansal sorry for the long wait.. I understand what you mean now, I actually had this issue not long ago. I ended up using a jQuery Plugin called [jQuery Caret](https://github.com/accursoft/caret). It's a very small plugin and works very easy. If you don't want to use jQuery you could even try using the plugin's source code, it's not that much. Hope that helps. – myfunkyside Jun 01 '17 at 11:59