A small question on javascript?

Why in functions the strong and em variables aren't visible textarea , textareaContent, coursorPosition
var textarea = document.querySelector(".textarea");
 var textareaContent = textarea.value;
 var coursorPosition = textarea.selectionStart;
strong.onclick = function(){
textarea.value = textareaContent.slice(0,coursorPosition) + "<strong>(Insert text instead of your text.)</strong>" + textareaContent.slice(coursorPosition);
}

em.onclick = function(){
 textarea.value = textareaContent.slice(0,coursorPosition) + "<em>(Insert your text instead of your text.)</em>" + textareaContent.slice(coursorPosition);
 }
March 12th 20 at 09:02
1 answer
March 12th 20 at 09:04
Solution
textareaContent and coursorPosition are set once when the script is executed. At the moment the textarea is empty, the cursor it is not.
Yes exactly thank you that's the way it works.
var textarea = document.querySelector(".textarea");
strong.onclick = function(){
let textareaContent = textarea.value;
let coursorPosition = textarea.selectionStart;
textarea.value = textareaContent.slice(0,coursorPosition) + "(Insert Tex instead of text.)" + textareaContent.slice(coursorPosition);
}

em.onclick = function(){
let textareaContent = textarea.value;
let coursorPosition = textarea.selectionStart;
textarea.value = textareaContent.slice(0,coursorPosition) + "(Insert Tex instead of text.)" + textareaContent.slice(coursorPosition);
} - dudley_Deckow commented on March 12th 20 at 09:07

Find more questions by tags JavaScript