2

I am trying to code something that allow user pressing enter and get the result of the request following the text inputted. The following code work fine with Chrome but not with IE 8 and 7 (I haven't trying the other version of IE) I used Jquery for the first time:

    function ifEnterClick(event) {
    if (event.keyCode == 13) {
        var elm = '#findButton';
        $(elm).click();
    }
}

and I tried javaScript too:

function ifEnterClick(event) {
    if (event.keyCode == 13) {
        var obj = document
                .getElementById("findButton");
        fireEvent(obj, 'click');
    }
}
function fireEvent(obj, evt) {

    var fireOnThis = obj;
    if (document.createEvent) {
        var evObj = document.createEvent('MouseEvents');
        evObj.initEvent(evt, true, false);
        fireOnThis.dispatchEvent(evObj);

    } else if (document.createEventObject) {
        var evObj = document.createEventObject();
        fireOnThis.fireEvent('on' + evt, evObj);
    }
}

In both case it work with Chrome but not IE.

oueslatibilel
  • 567
  • 1
  • 8
  • 24
  • How are you wiring ifEnterClick? I don't see any jquery being used here. This is straight-up javascript, and that's your "problem" I'm assuming. jQuery normalizes events and event handling for you, so if it works in one browser, 99.9% of the time it'll work in others. Switch this concept over to jQuery and I'm sure it'll work. – Eli Gassert Sep 18 '13 at 12:45
  • 2
    Which jq version are you using? jquery 2.x has removed old browser supports (IE7, IE8 e.g), maybe it is your issue here. BTW, jquery has normalized key events, you should use event.which, not event.keyCode – A. Wolff Sep 18 '13 at 12:47
  • A wolff, i am using version 2.12, i think u are right.. but even only JS is not wroking .. Eli Gassert, if (event.keyCode == 13) is working fine, the problem is in firing the event .click() – oueslatibilel Sep 19 '13 at 04:25
  • I found out something, click is fired in fact , i added onclick="alert('hi');" action="#{blabla.meth}" the alert is working, the problem is somewhere else, action is called fine too, its in rendering after.. – oueslatibilel Sep 19 '13 at 05:01

3 Answers3

2

Try this:

function ifEnterClick(event) {
  var keycode = (window.event) ? event.keyCode : event.which;
  if (event.keyCode == 13) {
    var elm = '#findButton';
    $(elm).click();
  }
}

Related: JavaScript KeyCode Values are "undefined" in Internet Explorer 8

Community
  • 1
  • 1
Ragnar123
  • 5,174
  • 4
  • 24
  • 34
  • in recnt jq version, it is normalized, just use event.which but that still a good answer! – A. Wolff Sep 18 '13 at 12:52
  • @A.Wolff True, but we dont know how the function `ifEnterClick` is used. The events are only normalized if it is bound with a jQuery event. For all we know, it may be called with inline event handlers. – Ragnar123 Sep 18 '13 at 12:58
  • if (event.keyCode == 13) is working fine , even in IE, I added one alert() and its working, the problem is $(elm).click(); – oueslatibilel Sep 19 '13 at 04:28
0

The problem is not click, its fired fine, or calling the action of the bottom. Its after the call and the submit.

oueslatibilel
  • 567
  • 1
  • 8
  • 24
-3

change

var elm = '#findButton';

to

var elm = $('#findButton');
Ganesh Pandhere
  • 1,612
  • 8
  • 11