I am implementing a spellcheker (corrector of words), I have a container where each new written word is placed in a new div, instead of placing it as ordinary text.
I am putting every word on a div, so that word suggestions are displayed, is this necessary? or can I put it in span? or on any label?
With div I managed the display of suggestions, when I try to span it does NOT work .
To see the functionality, type any word and press the space bar
let written = '';
document.getElementById('parent').addEventListener('keypress', function(e) {
written += String.fromCharCode(e.which);
if (e.which === 32) {
let newDivWord = document.createElement('div');
newDivWord.appendChild(document.createTextNode(written));
newDivWord.classList.add('root');
newDivWord.id = document.getElementById('parent').childElementCount + 1;
document.getElementById('parent').appendChild(newDivWord);
console.log(document.getElementById('parent'));
written = "";
}
})
#parent>div {
border-color: yellow;
float: left;
}
.root {
padding-right: 2px;
}
<div id="parent" contenteditable=true style="border: black 2px solid; height:200px">
<div class="root" id="1">Qué</div>
<div class="root" id="2">es</div>
<div class="root" id="3">Lorem</div>
<div class="root" id="4">Ipsum</div>
</div>
Should the written text that is not within the new div be erased?
The future functionality of my problem would be: , but if you help me, I would thank you infinitely.
The text What is Lorem Ipsum if I type 'hello' before lorem should be 'What is hello Lorem Ipsum', in a div of its own.
The text What is Lorem Ipsum if I type 'hello' after Ipsum should be 'What is Lorem Ipsum hello', on a div.
The text What is Lorem Ipsum if I type 'hello' before 'QUE' should be 'hello What is Lorem Ipsum hello', on a div of its own.
The text What is Lorem Ipsum if I type 'hello' before 'sum' should be 'hello What is Lorem Iphola sum hello', a div would be created for Iphola and sum would be another div.
What I need now in the present: I want the new word not to be repeated, one of them is the writing of the normal text, and the other repetition is the creation of the new div, and should add the new div at the end.