Skip to content Skip to sidebar Skip to footer

Input Type Text And Onkeydown Not Working Under Ie

I am writing a WWW application, it has to run under IE. I have the problem with the code that runs under Firefox, but I can't get it running under IE. // JS code function test() {

Solution 1:

That's strange, because your use of window.event should ensure your function only ever works in IE. That's certainly what happens for me when I try your code. I suspect there is more you're not showing us.

The usual way to handle events in a cross-browser way with inline event handler attributes would be:

<inputtype="text" name="item_code" onkeydown="test(event)">

functiontest(event) {
    if (event.keyCode===13)
        window.location.href= 'myPage.php';
}

This works because event in the attribute refers to the global window.event in IE, where in every other browser it refers to a local argument named event passed in to the event handler attribute function.

However, it is generally considered better to avoid event handler attributes and assign handlers from JavaScript itself. In this case you need a check to see which to use:

<input type="text" name="item_code">

// in a script block after the input, or in document-ready code//document.getElementsByName('item_code')[0].onkeydown= function(event) {
    if (event===undefined) event= window.event; // fix IEif (event.keyCode===13)
        window.location.href= 'myPage.php';
};

In general I would only try to trap Enter keypresses to reproduce/alter default form submission behaviour as a last resort; better if you can to let the form submit to the place you want it to go.

Solution 2:

try this. it has worked for me earlier.

 <input type="text" onKeyPress="KeyCheck"/>

 functionKeyCheck(e) {
    var KeyID = (window.event) ? event.keyCode : e.keyCode;
    if (KeyID == 13 ) {
       alert('key pressed!!);
    }
  }

Solution 3:

It should work in general. Two potential problems you may have run into:

  1. IE is known to do weird things when using relative links in location.href. Have you tried putting something like http://stackoverflow.com in there?
  2. Security settings in IE may prevent the right thing from happening if i.e. you are testing on a local file rather than a web server.

Solution 4:

jQuery fix

document.onkeydown = function (e) {
    varevent = jQuery.event.fix(e || window.event);
    var keycode = event.which;
    var isControl = event.ctrlKey;
}

Solution 5:

There is a way to make it work without using the <form> tag on your html code.

This is the simplest solution I found that works with IE, Chrome and Firefox at least:

$(function() {
  $("input#q").on('keydown', function(event) {
    if (event.which == 13) {
      document.location.assign("page.aspx?q=" + $("#q").val());
      returnfalse;
    }
  });
});
<inputname="q"type="text" />

Post a Comment for "Input Type Text And Onkeydown Not Working Under Ie"