1

I have attached the link to fiddle to simulate the problem I got.

eg: I entered 12345 > point the cursor in the middle 12|345 > type "space" > 123|45

the cursor will go backward after I type the "space".

Question: How can I make it stay at the same place as it is before?

Here is the link to fiddle

document.querySelector("#removeSpace").onkeyup =  function()
{

    var start = this.selectionStart,
        end = this.selectionEnd;

    this.value = this.value.replace(/\s/g,"");
    this.setSelectionRange(start, end);
};
2
  • Perhaps, like this. Although you still can enter spaces, but they will get removed. Commented Mar 8, 2018 at 10:47
  • cool, but can you explain a bit about this function, I appreciate that. function($0) { offset=$0.length; return ""; }) Commented Mar 8, 2018 at 10:55

1 Answer 1

1

You need to check if the whitespace was matched or not, and set the selection with the offset equal to the match length. However, to avoid the whitespace input, you may use input event:

$(function(){
  $('#removeSpace').bind('input', function(){
      var start = this.selectionStart,
            end = this.selectionEnd;
      var offset=0;
      this.value = this.value.replace(/\s+/g, function($0) {offset=$0.length; return "";});
      if (offset > 0)
    	   this.setSelectionRange(start-offset, end-offset);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="removeSpace" type="Text" />

The replace(/\s+/g, function($0) {offset=$0.length; return "";}) part matches 1 or more whitespaces and passes them to the callback via $0 variable. Its length is assigned to offset variable and is subtracted from the current start and end cursor positions. return "" removes the match, all the whitespaces found.

The same logic can be applied to fix your solution using keyup:

document.querySelector("#removeSpace").onkeyup =  function()
{
    
    var start = this.selectionStart,
        end = this.selectionEnd;
    var offset=0;
    this.value = this.value.replace(/\s+/g, function($0) {offset=$0.length; return "";});
    if (offset > 0)
    	this.setSelectionRange(start-offset, end-offset);
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="removeSpace" type="Text" />

Sign up to request clarification or add additional context in comments.

4 Comments

very helpful answer and well explained, thanks for provide the solution, appreciate that.
why is that this solution doesn't work in safari 5.1.7? the cursor will still going backward
@codingDummy I have no idea, I have never used Safari. I can't find any details on what there might be different. Perhaps, the input event is not supported. I added the code that should work for you even in Safari since it uses ECMAScript 5 regex related code, and is basically a fix for your code.
thanks for the answer, but onkeyup make things worse in safari, it will go back to the original problem. Well, thanks a lot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.