I want to change the value of the char at the index of the caret to "[br]".
example of what i mean:
lets say we have a textbox and a user enters some text, after a while he remembered that he forgot to start a new line, so he moves his caret to the location where he wants a new line and press return\enter. i want to add to that point the string "[br]", using javascript only. here is what i got now:
<textarea id="textarea" onkeydown="javascript:DoLine();"></textarea>
And here is the DoLine()
function:
function DoLine() {
if (event.keyCode == 13) {
var text = document.getElementById('textarea').value;
text += "[br]";
if (text.indexOf("[br]") != text.length) {
var getTextAfter = text[text.lastIndexOf("[br]")] += "\r";
}
document.getElementById('textarea').value = text;
}
}
this code adds the "[br]" at the end of the textarea instade of where the caret is. I dont really know how to get the index of the caret so i could just convert the text
var into an array of chars then add "[br]" at the index of the caret.
So anyone got any idea of how can I make my code work the way i wanted it to? or any idea how to get the caret location?
PS: by caret i mean this:
Thank you :)