Javascript set caret position in a ContentEditable div - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Div

Description

Javascript set caret position in a ContentEditable div

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=function(){/* w w w. ja  v a  2  s.  c o  m*/

var sel = window.getSelection();
var innerDiv = document.getElementById("innerKey");
var innerDivText = innerDiv.firstChild;
sel.collapse(innerDivText, 0);
innerDiv.parentNode.focus();

    }

      </script> 
   </head> 
   <body> 
      <div contenteditable="true" class="key"> 
         <div id="innerKey">
            276
         </div> 
      </div>  
   </body>
</html>

Related Tutorials