5

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?

James Donnelly
  • 126,410
  • 34
  • 208
  • 218
Arian
  • 12,793
  • 66
  • 176
  • 300
  • [http://stackoverflow.com/questions/4604930/changing-the-keypress/4605347#4605347](http://stackoverflow.com/questions/4604930/changing-the-keypress/4605347#4605347) – Victor Oct 20 '14 at 10:16

4 Answers4

3

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

Community
  • 1
  • 1
James Donnelly
  • 126,410
  • 34
  • 208
  • 218
  • Thanks.but that code does not solve my problem.When I hit `Enter` I want focus go to next control. – Arian Oct 20 '14 at 10:50
  • @Kerezo that's what I've answered in your other question: http://stackoverflow.com/a/26461581/1317805. You can combine this with `next()` if the `input` element is right next to the other `input` element, otherwise you're going to have to look for the element with a `tabIndex` one higher than the current element. For that, see this answer: http://stackoverflow.com/a/1803390/1317805. – James Donnelly Oct 20 '14 at 10:51
2

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

Salman A
  • 262,204
  • 82
  • 430
  • 521
  • It is working (checked in Chrome, IE11, IE8 emulated). When the focus is on the select element and enter is pressed, the next control is focused. – Salman A Oct 26 '14 at 08:28
  • I don't think it makes sense to have a textarea if you're using Enter to focus the next field. You would need to catch another key to make newlines in the textarea. – Rembunator Oct 26 '14 at 08:30
  • @Rembunator: just going with what OP asked. – Salman A Oct 26 '14 at 08:34
  • @SalmanA: That's fair. But using jQuery's :input (see my code), you can find all form elements in one selector. – Rembunator Oct 26 '14 at 09:00
  • I like how you used the on statement with a selector. I haven't seen this before. But wouldn't this put an event listener on every single element while it also always bubbles to the form element? – Rembunator Oct 26 '14 at 09:11
  • @Rembunator (i) using `:input` vs white listed elements does not matter. One could argue that `:input` is not a native CSS selector so it will be slow (ii) not it won't put a listener on every single element. There is only one handler! – Salman A Oct 26 '14 at 09:19
  • @SalmanA: There is no focus on `select` element because when I hit `Space` key, `select` element does not open – Arian Oct 26 '14 at 09:45
  • My browser never opens a select element when I hit space. Which browser are you using? – Rembunator Oct 26 '14 at 10:25
  • I'm sorry if this seems like nagging. But I was wondering which elements could normally exist, other than form elements, that would trigger a keypress event. And if they could, wouldn't you still want the next form element to be focussed. An exception perhaps for the submit elements. But they can be filtered out by the handler. There are 23 possibilities for the type of input element. 19 of them seem relevant for triggering the event. – Rembunator Oct 26 '14 at 11:36
0

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" />
Giannis Grivas
  • 3,374
  • 1
  • 18
  • 38
0

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.

Rembunator
  • 1,305
  • 7
  • 13
  • I added some code to prevent any submit button from being ignored. – Rembunator Oct 26 '14 at 08:55
  • @future_readers: FYI Any element with a tabIndex (also if tabIndex==0) is focusable (see [this thread](http://stackoverflow.com/questions/1599660/which-html-elements-can-receive-focus). Obviously using values above 0 (if the form is in a table for instance) will change how the next element is found. – Rembunator Oct 26 '14 at 12:02