Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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 — Postimage.org[^]

Can someone please explain how this can be done?

What I have tried:

html
<!DOCTYPE html>
<html>

<head>
  <script type="text/javascript" src="JavaS.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  <script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
  <meta charset="utf-8" />
  <style>
   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;
    }
   </style>
   </head>
   <body>

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

<script> setfocus(); </script>

</body>

</html>


JavaScript JavaS.js
// first row cursor position in textarea inputbox on page load
function setfocus() {
  document.getElementById('box').focus();
}

// 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 
    event.preventDefault(); 

    // creates child div for output
    var div2 = document.createElement("div");
    div2.setAttribute("contenteditable", "false");
    // div2.setAttribute("tabindex", "1");
    div2.style.background = "lightgreen";
    div2.style.height = "auto";
    div2.style.width = "600px";
    document.getElementById("box").appendChild(div2);

    // 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");
    div3.style.height = "20px";
    div3.style.width = "600px";
    document.getElementById("box").appendChild(div3);
    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;
}
Posted
Updated 12-Sep-19 6:56am

1 solution

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

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900