Quantcast
Channel: Active questions tagged html - Stack Overflow
Viewing all articles
Browse latest Browse all 72358

Simplest way to add text in front of cursor at cursor position, jQuey, HTML textarea [duplicate]

$
0
0

I am trying to make a simple in browser text editor and would like to place a ")" when the user types "(" and would like to place the cursor between them, like in VSCode. This also needs to happen at the cursors position within the text area.

This is what I have so far:

<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("keydown", function(e) {
            if (e.which == 57) {
                $("#input").focus();
                // and just run the command
                document.execCommand('insertText', false /*no UI*/ , ")");
            }
        });
    </script>

</body>

However this inserts the closed bracket behind the open bracket and places the cursor and is not what I want. Is it possible to this and if so what is the simplest way


Viewing all articles
Browse latest Browse all 72358

Trending Articles