Steve Jones
Steve Jones

Reputation: 117

Delete Key Move Focus to Previous Input Field

I'm have a phone number input with 10 input fields that when you fill in each number it skips to the next.

I would like it to be able to go back to the previous field when I hit the delete key (for example I entered an incorrect digit in my phone number). How can I do this?

Check out my CodePen for it: http://codepen.io/Steve-Jones/pen/qdMjWb/

HTML

<script src="https://code.jquery.com/jquery-1.9.1.min.js"></script>
<body>
  <div class="phone-field"> 
    (<input class="phone-input" name="phone-input" type="tel" size="1" maxlength="1" placeholder="5" autofocus="autofocus">
    <input class="phone-input" name="phone-input" type="tel" size="1" maxlength="1" placeholder="5">
    <input class="phone-input" name="phone-input" type="tel" size="1" maxlength="1" placeholder="5">)
    <input class="phone-input" name="phone-input" type="tel" size="1" maxlength="1" placeholder="5">
    <input class="phone-input" name="phone-input" type="tel" size="1" maxlength="1" placeholder="5">
    <input class="phone-input" name="phone-input" type="tel" size="1" maxlength="1" placeholder="5"> -
    <input class="phone-input" name="phone-input" type="tel" size="1" maxlength="1" placeholder="5">
    <input class="phone-input" name="phone-input" type="tel" size="1" maxlength="1" placeholder="5">
    <input class="phone-input" name="phone-input" type="tel" size="1" maxlength="1" placeholder="5">
    <input class="phone-input" name="phone-input" type="tel" size="1" maxlength="1" placeholder="5">
</body>

CSS

.phone-field {
  margin: 50px;
  font-size: 20px;
}

.phone-input {
  width: 13px;
  text-align: center;
  font-size: 16px !important;
  height: 1.75em;
  border: 0;
  outline: 0;
  background: transparent;
  border-bottom: 2px solid #ddd;
  margin-right: 2px;
  margin-left: 2px;
}

[placeholder]:focus::-webkit-input-placeholder {
  transition: opacity 0.5s 0.0s ease;
  opacity: 0;
}

jQuery

  $(document).ready(function(){
    $('body').on('keyup', 'input.phone-input', function(){
      if($(this).val().length === this.size){
        var inputs = $('input.phone-input');
        inputs.eq(inputs.index(this) + 1).focus();
      }
    });
  });

Upvotes: 4

Views: 5435

Answers (3)

divy3993
divy3993

Reputation: 5810

It's Possible by key codes: CodePen

$(document).ready(function(){

    $('body').on('keyup', 'input.phone-input', function()
    {
      var key = event.keyCode || event.charCode;
      var inputs = $('input.phone-input');
      if(($(this).val().length === this.size) && key != 32)
      {
        inputs.eq(inputs.index(this) + 1).focus();  
      } 
      if( key == 8 || key == 46 )
      {
        var indexNum = inputs.index(this);
        if(indexNum != 0)
        {
        inputs.eq(inputs.index(this) - 1).val('').focus();
        }
      }

    });
  });

Looking at your code i found one more thing, on space-bar cursor goes to next index. So also to cover that, i think this is suitable.

KeyCodes

Backspace = 8

Delete = 46

Space Bar = 32

Upvotes: 3

Thanh Trinh
Thanh Trinh

Reputation: 1

Just check key event is delete and then focus before element. I update your js file as below and it worked well.

$(document).ready(function(){
    $('body').on('keyup', 'input.phone-input', function(e){
      if($(this).val().length === this.size){
        var inputs = $('input.phone-input');
        inputs.eq(inputs.index(this) + 1).focus();
      }
      if(e.keyCode == 8 || e.keyCode == 46){
        var inputs = $('input.phone-input');
        inputs.eq(inputs.index(this) - 1).focus();
      }
    });  
  });

Upvotes: 0

DasBeasto
DasBeasto

Reputation: 2292

http://codepen.io/anon/pen/XbPgjJ

Just watch for the backspace key and move backwards instead of forward.

  if(e.keyCode == 8){
    var index = inputs.index(this);
    if (index != 0)
        inputs.eq(index - 1).val('').focus();    
  }

Upvotes: 1

Related Questions