Replace keyCode in IE 11

We use this code for simulating Tab key with Enter key:

function EnterTab() {
    if (event.keyCode == 13) event.keyCode = 9;
    return false;
}

But in IE 11 keyCode is readonly and this code does not work. How I can solve this problem that my code run in IE 11?

Answers:

Answer

We use this code for simulating Tab key with Enter key:
...
When I hit Enter I want focus go to next control.

The idea is wrong. Don't change enter to tab. Tackle this problem by intercepting the enter key, set the focus on next element and cancel the event. jQuery solution:

$(function() {
  $("form").on("keypress", "input[type=text], select, textarea", function(e) {
    if (e.which === 13) {
      e.preventDefault();
      var $fields = $(this).closest("form").find(":input");
      var index = $fields.index(this);
      $fields.eq(index + 1).trigger("focus");
    }
  });
});
input,
select,
textarea {
  box-sizing: border-box;
  margin: .5em 0;
  width: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

<form method="post">
  <p>Hitting enter inside the text fields will not submit the form</p>
  <textarea rows="4"></textarea>
  <input type="text" />
  <select>
    <option>1</option>
    <option>2</option>
    <option>3</option>
  </select>
  <select>
    <option>A</option>
    <option>B</option>
    <option>C</option>
  </select>
  <input type="text" />
  <input type="submit" value="sumbmit" />
</form>

jsFiddle for testing in Internet Explorer

Answer

keyCode should be readonly in all modern browsers, simply because modifying it is hacky and unnecessary. Looking at the code you provided in your other question: keydown event does not fire correctly using jQuery trigger, we can imitate this functionality rather than attempt to return the altered event.

jQuery actually does let us modify the event object when it is passed in to a jQuery event handler. As you have tagged, we can therefore make use of this in our answer, passing the modifications into a trigger() called on the same element.

if (e.which === 13) {
    // Alter the normalised e.which property
    e.which = 9;

    // Alter the default properties for good measure
    e.charCode = 9;
    e.keyCode = 9;

    $(this).trigger(e);
    return false;
}

Note: I've used e.which rather than e.keyCode as jQuery normalises this across all browsers.

Here is a demo which shows this in action. If you press the Enter key when the input element is in focus, the event will be replaced with the Tab event:

var $input = $('input'),
    $p = $('p');

$input.on('keydown', function(e) {
    if (e.which == 13) {  
        $p.html($p.html() + 'Enter key intercepted, modifying 13 to 9.<br>');
                
        e.which = 9;
      
        e.charCode = 9;
        e.keyCode = 9;
      
        $(this).trigger(e);
      
        return false;
    }
  
    $p.html($p.html() + 'Key pressed: ' + e.keyCode + '<br>');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="text" />
<p></p>

As I've mentioned in my answer here, just because you're changing the event doesn't mean your browser is then going to handle it as if the Tab key has been pressed. The logging lets us know that our event has successfully been intercepted and changed from Enter to Tab, even on Internet Explorer 11.

IE11 Example

Answer

This worked in IE9 but no longer works in IE11.So you have to find a solution that stimulates the result you want.For example, if you want to allow users to press 'Enter' to go to the next data entry field (like tab does), try something like this.

var i = 0;
var els = myform.getElementsByTagName('input').length
document.onkeydown = function(e) {
  e = e || window.event;
  if (e.keyCode == 13) {
    i++;
    i > els - 1 ? i = 0 : i = i;
    document.myform[i].focus();

  }
};
<span>Press 'enter' inside text to act like tab button</span>
<form name="myform">
  <input type="text">
  <input type="text">
  <input type="text">
</form>

jQuery Example:

$('.info').bind('keypress', function(event) {
  if (event.which === 13) {
    var nextItem = $(this).next('.info');

    if (nextItem.size() === 0) {
      nextItem = $('.info').eq(0);
    }
    nextItem.focus();
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" class="info" />
<input type="text" class="info" />
<input type="text" class="info" />

Answer

You can just select the next or previous form element after catching the Enter key.

This will catch all possible form elements. Pressing shift-Enter will move the focus to the previous form element.

Pressing Enter on the submit button will still submit the form. Pressing shift-Enter will focus the previous form element.

function EnterTabTest(event) 
    {
    if (!event.shiftKey && (event.target.getAttribute('type')=='submit')) return true;

    event.preventDefault();
    if (event.keyCode == 13)
        {
            if (event.shiftKey)
                $(event.target).prevAll(":input").first().focus();
            else
                $(event.target).nextAll(':input').first().focus();
        }
    return false;
    }


<form onkeydown='EnterTabTest(event)'>
    <input type='text' />
    <input type='text' />
    <input type='text' />
    <p>not part of the form</p>
    <select>
        <option>opt1</option>
        <option>opt2</option>
    </select>
    <p>also not part of the form</p>
    <input type='text' />
    <input type='submit' />

</form>

Here is a fiddle so you can try

It also works in IE11, I tried.

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.