Suppose I have a plain HTML textarea. What I want to do is I want to be able to insert a different character when a character is typed. For example, I want to insert <>
when <
is typed into the textarea. Also, I want to be able to move the cursor between the <
and >
if possible.
Here's what I have for now. It only works if the cursor is at the end though.
let editing = document.querySelector("#editing");
const handler = (event) => {
if (editing.value.endsWith("<")) {
editing.value += ">";
editing.setSelectionRange(editing.selectionStart - 1, editing.selectionStart - 1)
}
}
editing.addEventListener("propertychange", handler)
editing.addEventListener("input", handler)
<textarea name="editing" id="editing"></textarea>