Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

245
Views
how to change the cursor position in textarea

I want to make bracket auto closing function inside textarea using react. The cursor should be inside the bracket after auto closing. But when the function is called, cursor go to the end of the textarea value. I have no idea why it happens and there is no working solution about setting cursor. How can I solve it?

I'm using Editor from 'react-simple-code-editor'


 function bracketClose(e) {
    var selectPos=e.target.selectionStart;
    var wholeText=document.getElementById("solution").value;
    var beforeText=wholeText.substring(0,selectPos);
    var closeBracket="";
    var afterText=wholeText.substring(selectPos,wholeText.lenth);
    const close=e.key;

    if(close==="["){
      closeBracket="]";
    }
    else if (close==="{"){
      closeBracket="}";
    }
    else if (close==="<"){
      closeBracket=">";
    }
    else if (close==="("){
      closeBracket=")";
    }
    else if (close==="'"){
      closeBracket="'";
    }
    else if(close==="\""){
      closeBracket="\"";
    }

    wholeText= beforeText+closeBracket+afterText;
    setCode(wholeText);
    e.target.setSelectionRange(selectPos,selectPos);
    document.getElementById("solution").focus();
  };

<...>
 <Editor
    className="editor"
    textareaId="solution"
    value={code}
    onKeyUp={bracketClose}
    onValueChange={code => setCode(code)}
    highlight={code => hightlightWithLineNumbers(code, languages.js)}
 />
<...>
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!