-1

I am using this function to limit characters in a textarea. Works well on all browsers minus IE10 (refuse to test on anything lower).

This part $(target).text(num - len); places the actual number into the textarea, not the limit.

Can anyone point to a fix for IE10 please, im just not that versed with microsoft products. thank you

function countChar(val, target, num){
    var len = val.value.length;
    if (len >= num) {
            val.value = val.value.substring(0, num);
            $(target).text(0);
    }else {
            $(target).text(num - len);
    }

};

here is my fiddle: http://jsfiddle.net/nalagg/uFDhZ/

on fiddle IE is not responding to the limit

edit reversing order seems to have fixed it:

$(target).text(0);
val.value = val.value.substring(0, num);
t q
  • 4,593
  • 8
  • 56
  • 91

1 Answers1

0

I'm not able to test it with IE but try this:

$('#myTxtArea').on('keyup propertychange', function(){
   countChar(this, '#myTxtArea', 10);
});

EDIT

I'm not sure why it's not working but I guess the event handler isn't recognized by IE for some reasons.

Could you try if the following works for you?

jsfiddle

$('#myTxtArea').on('keypress', function(e){

    e = e || window.event;

    var tval = $('#myTxtArea').val(),
        tlength = tval.length,
        set = 10,
        remain = parseInt(set - tlength);

        $('p').text(remain);

        if (remain <= 0 && e.which !== 0 && e.charCode !== 0) {
            $('textarea').val((tval).substring(0, tlength - 1))
        }
});
damian
  • 5,314
  • 5
  • 34
  • 63
  • that didnt change anything – t q Dec 17 '13 at 18:17
  • Im wondering if fiddle has issues on IE because im getting the same result, nothing changed. will make an actual html/js page to test – t q Dec 17 '13 at 18:37