1

I have 3 input fields on my UI as in which one of them is readonly so after entering something in my first input field when I press enter it goes directly to 3rd one which is editable

What I am trying is when I am in 3rd Input field and there I press shift+tab I want to go back to first Input field which is editable, I have tried many things on google but not working

$(document).keypress(function(event) {
  var keycode = event.keyCode || event.which;
  if (keycode == '13') {
    if (event.target.matches('[name=hsnCodeInput]')) {
      $("#mbqInput").focus();
    }

  }

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<div class="row">
  <div class="form-group col-xs-6 col-sm-6 col-md-6 col-lg-2">
    <label for="hsnCodeInput">HSN Code</label> <input type="text" class="form-control" id="hsnCodeInput" name="hsnCodeInput">
  </div>
  <div class="form-group col-xs-6 col-sm-6 col-md-6 col-lg-2">
    <label for="searchCodeInput">Search Code</label> <input type="text" class="form-control" id="searchCodeInput" name="searchCodeInput" readonly="readonly">
  </div>
  <div class="form-group col-xs-6 col-sm-6 col-md-6 col-lg-2">
    <label for="mbqInput">MBQ</label> <input type="text" min="0" class="form-control" id="mbqInput" name="mbqInput">
  </div>
</div>
vivek singh
  • 417
  • 2
  • 12
  • 36

2 Answers2

3

tabindex="-1" on readonly solves this problem

A negative value (usually tabindex="-1") means that the element should be focusable, but should not be reachable via sequential keyboard navigation. It's mostly useful to create accessible widgets with JavaScript.

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<div class="row">
  <div class="form-group col-xs-6 col-sm-6 col-md-6 col-lg-2">
    <label for="hsnCodeInput">HSN Code</label> <input type="text" class="form-control" id="hsnCodeInput" name="hsnCodeInput">
  </div>
  <div class="form-group col-xs-6 col-sm-6 col-md-6 col-lg-2">
    <label for="searchCodeInput">Search Code</label> <input type="text" class="form-control" id="searchCodeInput" name="searchCodeInput" tabindex="-1" readonly="readonly">
  </div>
  <div class="form-group col-xs-6 col-sm-6 col-md-6 col-lg-2">
    <label for="mbqInput">MBQ</label> <input type="text" min="0" class="form-control" id="mbqInput" name="mbqInput">
  </div>
</div>
User863
  • 19,346
  • 2
  • 17
  • 41
2

Answer to prevent select readonly: tabindex="-1" can prevent to select readonly textbox,

How to check tab or shift + tab pressed and Tab feature is working fine to select next element, without any changes, and if we want to do some specific functions like, to click specific button or trigger specific function or plugin, then we have to manipulate the tab feature.

So for that I write following code, might help

FIY

When tab pressed key is 9 When Shift+tab pressed key is 16

Enjoy the code, Happy to help

  document.addEventListener('keyup', function(event){
  var keycode = event.keyCode || event.which;
  if (keycode === 9) {
    // tab pressed
    alert('Tab pressed')

  }

  if (keycode === 16) {
      // tab + shift pressed

      alert('Tab + Shift pressed')
    }
  });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<div class="row">
  <div class="form-group col-xs-6 col-sm-6 col-md-6 col-lg-2">
    <label for="hsnCodeInput">HSN Code</label> <input type="text" class="form-control" id="hsnCodeInput" name="hsnCodeInput">
  </div>
  <div class="form-group col-xs-6 col-sm-6 col-md-6 col-lg-2">
    <label for="searchCodeInput">Search Code</label> <input type="text" class="form-control" id="searchCodeInput" name="searchCodeInput" readonly="readonly" tabindex="-1">
  </div>
  <div class="form-group col-xs-6 col-sm-6 col-md-6 col-lg-2">
    <label for="mbqInput">MBQ</label> <input type="text" min="0" class="form-control" id="mbqInput" name="mbqInput">
  </div>
</div>
Dupinder Singh
  • 7,175
  • 6
  • 37
  • 61