Word counter not working in IE

I’m new to Javascript. I set up a word counter on a form using Javascript. It works great in all browsers except, you guess it - IE. In both IE9 and IE11 the counter is unreliable, and the whole field can ‘freeze up’ and not allow you to click in it and edit anything.

Does anyone know why it’s breaking in IE? And if it can’t be fixed in a simple way, is there a way to conditionally use the script only if the browser is not IE?

Here’s a demo of the form: http://nzensandbox.wpmakeover.com/

And the code:

$("#field_advstatement").on('keydown', function(e) {
        var words = $.trim(this.value).length ? this.value.match(/\S+/g).length : 0;
        if (words <= 50) {
            $('#display_count2').text(words);
            $('#word_left2').text(50-words)
        }else{
            if (e.which !== 8) e.preventDefault();
        }
    });

Any help would be greatly appreciated.

Hi,

I can only test in IE10 and there it works fine, so this is an odd bug.

One way to bedug this would be to remove everything, then add it back line for line, until you find out exactly what is breaking.

E.g.

$("#field_advstatement").on('keydown', function(e) {
    var words = $.trim(this.value).length;
    console.log(words,);
    Console.log(typeofwords);
});

This would be a good starting point.
What does this log to the console in one of the browsers where its not working?