1

I need / (divide) to act as tab key (switch between inputs) and . (decimal) to act as backspace (delete characters inside text box) using pure JavaScript or JavaScript and jQuery, CSS and HTML. How can I achieve this?

Maybe with script like this?

document.addEventListener('keyup', function (e) {
      if (e.keyCode == 111) {
        var e2 = jQuery.Event("keydown");
        e2.which = 8; 
        e2.keyCode = 8;
        $("input").trigger(e2);
      }
    });

And this for the tab thing

document.addEventListener('keyup', function (e3) {
      if (e3.keyCode == 110) {
        var e4 = jQuery.Event("keydown");
        e4.which = 9; 
        e4.keyCode = 9;
        $("input").trigger(e4);
      }
    });
  • Please provide a Minimal, Reproducible Example: https://stackoverflow.com/help/minimal-reproducible-example – Twisty Jun 17 '20 at 18:28

1 Answers1

0

Consider the following example.

$(function() {
  function alterText(evt) {
    var c = evt.which;
    var t = $(evt.target);
    if (c !== 46 && c !== 47) {
      return true;
    }
    evt.preventDefault();
    if (c == 46) {
      t.val(t.val().slice(0, -1));
    }
    if (c == 47) {
      t.next().focus();
    }
    return false;
  }

  $("input").keypress(alterText);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" id="part-1" />
<input type="text" id="part-2" />
<input type="text" id="part-3" />

You can use .keypress(), see more here: Simulate pressing tab key with jQuery

If the which is 46 (.) or 47 (/) we can mimic specific operations.

Twisty
  • 30,304
  • 2
  • 26
  • 45