I am making a text editing document and I have a function which automatically places ")" when the user types "(". However this places the cursor outside of the closing bracket. So I would like to move the cursor back one character so that the user can type within the brackets. This is my current code:
<body>
<textarea name="input" id="input" cols="30" rows="10"></textarea>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$("#input").on("keyup", function(e) {
if (e.which == 57) {
$("#input").focus();
// and just run the command
document.execCommand('insertText', false /*no UI*/ , ")");
}
});
</script>
</body>
What is the simplest way to accomplish this?