Answers for "how to autoclose paranthesis in javascript input"

0

how to autoclose paranthesis in javascript input

const closeChars = new Map([
  ['{', '}'],
  ['[', ']'],
  ['(', ')']
]);
    
maintextarea=document.getElementById('maintextarea');

maintextarea.addEventListener('input', function (e) {

    const pos = e.target.selectionStart;
    const val = [...e.target.value];
    
    const char = val.slice(pos-1, pos)[0];
    const closeChar = closeChars.get(char);
    
    if (closeChar) {
      val.splice(pos, 0, closeChar);
      e.target.value = val.join('');
      e.target.selectionEnd = pos;
    }
});
Posted by: Guest on May-07-2021

Code answers related to "how to autoclose paranthesis in javascript input"

Code answers related to "Javascript"

Browse Popular Code Answers by Language