trying to create a Richt Texteditor in Javascript. I have a Tag
<span class="text-3xl">Foo</span>
and want to edit it to something like
<span class="text-3xl">F</span><span class="text-sm">o</span><span class="text-3xl">o</span>
My first attempt works, for inserting a Tag inside the other
<span class="text-3xl">F<span class="text-sm">o</span>o</span>
using this code:
var spanElement= document.createElement("span");
spanElement.className = size;
var selection= window.getSelection().getRangeAt(0);
var selectedText = selection.extractContents();
elem.appendChild(selectedText);
selection.insertNode(elem);
but need some help with closing/reopening the tag before/after the inner tag.
2
Answers
After reading some other threads, espacially Javascript: I can get the text of a selection, now how do I get the text outside the selection?
is my temp solution
That works as expected. Im in a learning process, coming from C#, so this feels really hacky and i think i need to read a bit/lot more to get it to a usable solution. So, every advise and/or recommendation to JS Books would be a great help too
Once you have these two arrays:
['F', 'o', 'o']
['text-3xl', 'text-sm', 'text-3xl']
You can build the string:
You’ll need to declare the class Array explicitly in your JavaScript.
But you can extract the character Array from your original
<span>
.Working Example:
Further Reading:
The example above uses a backtick-demarcated
template literal
: