Skip to content Skip to sidebar Skip to footer

How To Simulate TAB On ENTER Keypress In Javascript Or JQuery

I want to change keycode in keydown ( key press ) in all input in a page.I want to replace Enter keycode with TAB key code. How I can do this? thanks EDIT 1) Consider this code: &

Solution 1:

I've had a similar problem, where I wanted to press + on the numpad to tab to the next field. Now I've released a library that I think will help you.

PlusAsTab: A jQuery plugin to use the numpad plus key as a tab key equivalent.

Since you want enter/ instead, you can set the options. Find out which key you want to use with the jQuery event.which demo.

JoelPurra.PlusAsTab.setOptions({
  // Use enter instead of plus
  // Number 13 found through demo at
  // https://api.jquery.com/event.which/
  key: 13
});

Then enable the feature by adding plus-as-tab="true" to the form fields you want to use enter-as-tab in, or some other element that contains these form fields. Radio buttons should not be a problem, as they are covered by my other library, EmulateTab - see autonavigation of radio buttons in that demo.

<div plus-as-tab="true">
  <!-- all focusable elements inside the <div> will be enabled -->
  <asp:RadioButtonList ID="RadioButtonList1" runat="server">
    <!-- Radio buttons should not be a problem. -->
  </asp:RadioButtonList>
</div>

You can try it out yourself in the PlusAsTab enter as tab demo.


Solution 2:

This code is to replace enter with tab character:

$("#wmd-input").bind("keypress", function(e) {
   if (e.keyCode == 13) {
      var input = $(this);
      var inputVal = input.val();
      setTimeout(function() {
        input.val(inputVal.substring(0,inputVal.length) + "\t");
      }, 1);
   }
});

Live Demo

UPDATE:

This code is to focus to on the next element:

$(document).ready(function () {
    $("input,select").bind("keydown", function (e) {
        if (e.keyCode == 13) {
            var allInputs = $("input,select");
            for (var i = 0; i < allInputs.length; i++) {
                if (allInputs[i] == this) {
                    while ((allInputs[i]).name == (allInputs[i + 1]).name) {
                        i++;
                    }

                    if ((i + 1) < allInputs.length) $(allInputs[i + 1]).focus();
                }
            }
        }
    });
});

Solution 3:

Hope this works

$('input,textarea').keydown(function(){
  if(event.keyCode==13) {
    event.keyCode = 9;
  }
});

Edit

Try this http://jsfiddle.net/GUmUg/. Play around with selectors to make this work as i don't know asp

$('input,textarea').keypress(function(e){
    if(e.keyCode==13) {
   $(this).next().focus();
  }
});

Solution 4:

$('input').on('keydown',function(e){
    var keyCode = e.keyCode || e.which;
     if(e.keyCode === 13) {
      e.preventDefault();
      $('input')[$('input').index(this)+1].focus();
      }
});

check fiddle here : http://jsfiddle.net/Pd5QC/


Solution 5:

The way I do it is by using jquery to each over your selection and focusing on the element after the current on you are on.

$(document).on('keyup', '.my-input', function (ev) {

    if (ev.keyCode == '13') {
        var currentInput = this;
        var isOnCurrent = false;

        $('.my-input').each(function () {

            if (isOnCurrent == true) {
                $(this).focus();
                return false;
            }

            if (this == currentInput) {
                isOnCurrent = true;
            }

        });
    }
});

Post a Comment for "How To Simulate TAB On ENTER Keypress In Javascript Or JQuery"