1const closeChars = new Map([
2 ['{', '}'],
3 ['[', ']'],
4 ['(', ')']
5]);
6
7maintextarea=document.getElementById('maintextarea');
8
9maintextarea.addEventListener('input', function (e) {
10
11 const pos = e.target.selectionStart;
12 const val = [...e.target.value];
13
14 const char = val.slice(pos-1, pos)[0];
15 const closeChar = closeChars.get(char);
16
17 if (closeChar) {
18 val.splice(pos, 0, closeChar);
19 e.target.value = val.join('');
20 e.target.selectionEnd = pos;
21 }
22});