Home > OS >  shift Question mark (?) a place left after converting dot (.) into (?)
shift Question mark (?) a place left after converting dot (.) into (?)

Time:12-14

I have code that converts (.) into (?) but I want to shift it a place left after converting dot (.) into (?) , if we write [abcdef .] it should become [abcdef?] after pressing dot key that removes a space in left side, if we write [abcdef.] it should become [abcde?] after pressing dot key that removes a nearest character in left side

<!doctype html>
<html dir='ltr' lang='en-GB'>

<head>
  <meta charset="UTF-8">
  <title>test page</title>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
 
<script language="javascript" type="text/javascript">

  
  $(function() {
  $('textarea').on("keyup", function(e) {
    if (e.key === '.') {
      const index = this.selectionStart;
      const text = $(this).val();
      if (index > 0 && text.charAt(index - 1) === '.') {
        $(this).val(text.substr(0, index - 1)   '?'   text.substr(index));
        this.selectionStart = index;
        this.selectionEnd = index;
      }
    }
  });
});
</script>
</head>

<body>
  <textarea></textarea>
</body>

</html>

CodePudding user response:

Just change:

$(this).val(text.substr(0, index - 1)   '?'   text.substr(index));

To:

$(this).val(text.substr(0, index - 2)   '?'   text.substr(index));

That line is setting the value of the textarea, and it is taking the substring of original text from 0 to the position of '.' (index). If you change to minus 2 it will take an extra character before the '.' as you asked.

  • Related