kach
kach

Reputation: 809

Event Change in IE not working when pressing Enter Button

I want to execute a function in change of an input[type=text], this works fine in all Browsers except in IE.

This is an exemple :

$('input.switch').change(function(){
    alert('ok now');    
});

the text input looks like that :
<input class="switch" type="text" value="50000" />

Don't forget that i have many inputs with different classes !

Do you have in solutions for this ?

Upvotes: 4

Views: 5329

Answers (2)

Dunc
Dunc

Reputation: 18932

Here's a more targeted polyfill for IE:

var isIE = !!document.documentMode;
if (isIE) {

    $("body").on("keydown", "input[type='text']", function (e) {

        // Ensure 'change' event is fired on enter in IE
        if ((e.keyCode || e.which) === 13) {
            $(this).blur();
        }
    });
}

Upvotes: 2

Naor
Naor

Reputation: 24093

If you wish to activate an event when pressing Enter then register this event:

$('input.switch:first').keydown(function(e){
    if (e.keyCode==13) { //Enter keycode
        //Do you stuff
    }
});

Enter doesn't activate change maybe because the text input doesn't lose focus.

Good luck!

Update: The latest version of jQuery allows you to get the focused element using: $('*:focus'). I believe you should add script that will support IE. For each keydown, if the key is Enter, get the focused input, blur it and trigger change:

$('body').keydown(function() {
    if (e.keyCode==13) { //Enter keycode
        $('input:focus').blur().change(); //The blur is to prevent change happen again
    }
});

I am not sure you can force IE to trigger change when Enter pressed in different way.

Hope this helps this time.

Upvotes: 3

Related Questions