Click here to Skip to main content
15,217,723 members
Rate this:
Please Sign up or sign in to vote.
When a user push enter in an editable div the cursors position (text caret) should move two rows down instead of one because the second row (div child) will hold the output (non editable) and the third row will hold the new input div (editable) where the cursor now should be located.

I have made a simple picture to illustrate.

code project —[^]

Can someone please explain how this can be done?

What I have tried:

<!DOCTYPE html>

  <script type="text/javascript" src="JavaS.js"></script>
  <script src=""></script>
  <script src=""></script>
  <meta charset="utf-8" />
   div:focus {
    background-color: Aqua;
    #box {
      background-color: lightblue;
      width: 600px;
      height: 600px;
      border: none;
      font-size: 15px;
      resize: none;
      overflow: auto;
      overflow-wrap: break-word;

  <div id="box" contenteditable="true" onkeypress="parse(event, this)"></div>

<script> setfocus(); </script>



JavaScript JavaS.js
// first row cursor position in textarea inputbox on page load
function setfocus() {

// counts the number of times the user push enter
function increment() {
  increment.n = increment.n || 0;
  console.log("increment.n = " + increment.n);
  return ++increment.n;

function parse(e) {
  var key = window.event.keyCode;
  if (key == 13) { //keycode for enter 

    // creates child div for output
    var div2 = document.createElement("div");
    div2.setAttribute("contenteditable", "false");
    // div2.setAttribute("tabindex", "1"); = "lightgreen"; = "auto"; = "600px";

    // assign output to child div 
    var input = document.getElementById('box').innerText;
    console.log("input = " + input); 
    var output = eval(input);
    console.log("output = " + output);
    div2.innerHTML = output;

    // creates new child div for input
    var div3 = document.createElement("div");
    div3.setAttribute("id", "div3"); = "20px"; = "600px";
    div3.setAttribute("tabindex", "0");

// an array with random numbers between -1 and 1
function rand(n) {
  x = [];
  for (var i = 0; i < n; i++) {
    x[i] = Math.random() * 2 - 1;
  console.log("x = " + x);
  return x;
Updated 12-Sep-19 6:56am

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

People usually get an "edit" or "add" button; you're trying to create a desktop app in a browser. Bad idea.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100