I am working on my HTML text editor to input the texts in the contenteditable and create the div tags automatically after when I input the texts and hit on the enter button of the keyboard.
When I type the text “Test” in the contenteditable and when I hit on the enter button, I am getting this:
Test<div><br></div>
Here is what I want to achieve:
<div dir="auto">Test</div><div dir="auto"><br></div>
In the contenteditable, it will only add the div tags in the new line with <br>
tag after when I hit on the enter button of the keyboard. It wont add the div tag with the text, it will only add the div tag with <br>
tag.
I want to add <div dir="auto">
tag in the contenteditable everytime when I input the text in the new line and when I hit on the enter buttons of the keyboard to add the <br>
tag inside the div tag. If the texts are already inside the div tag, there would be no need for the div to be add automatically.
Here is the html code:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,400i,700|Roboto+Mono">
</head>
<body leftmargin="0" topmargin="0" rightmargin="0" bottommargin="0">
<div id="main" class="main" style="padding-left: 13px;"><div id="editor" contenteditable="true"></div></div>
<script type="text/javascript" src="richeditor.js"></script>
</body>
</html>
JS:
// Event Listeners
RE.editor.addEventListener("input", RE.callback);
addEventListener("keyup", function(e) {
var KEY_LEFT = 37, KEY_RIGHT = 39;
var KEY_ENTER = 13;
keypress = KEY_ENTER;
if (e.which == KEY_LEFT || e.which == KEY_RIGHT) {
RE.enabledEditingItems(e);
}
if (e.which == KEY_ENTER) {
window.scrollTo(0, document.body.scrollHeight);
}
else
{
keypress = e.which;
}
endofLine = checkLine();
});
CSS:
#editor {
display: table-cell;
width: 100%;
outline: 0px solid transparent;
background-repeat: no-repeat;
background-position: center;
background-size: cover;
}
I tried to find on google how I could add the div tags in the contenteditable automatically after when I input the texts, but I couldn’t find it.
Can you please show me an example how I could add the div tag <div dir="auto">
in the contenteditable automatically when I input the new text in the new line and when I hit on the enter button of the keyboard the div will also add with the <br>
tag??