Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IE Error, Object doesn't support this property or method

 function charCount(){
  $.doTimeout('poll', 150, function(){
      messageVal = $('#messageLabel textarea').val();

      messageLength = messageVal.length; //IE BREAKS HERE

      $('#messageLength').html(messageLength + '/140')
      if(messageLength > 140){
          $('#messageLength').not('.inv').addClass('inv')
      }else{
          $('#messageLength.inv').removeClass('inv')
      }
      return false;
  })

}
$('#messageLabel textarea').change(charCount).keyup(charCount);

Gives the following error in Internet Explorer 7.0 (and maybe other versions too).

Object doesn't support this property or method.

Any ideas on what is causing this error?

like image 465
dubbelj Avatar asked Apr 13 '11 09:04

dubbelj


1 Answers

When you don't use the var keyword, IE browser search for messageLength in the global context and it finds it... you have element with that ID.

Trying to assign number to HTML element fails.

To solve this, just declare messageLength as local variable:

var messageLength = messageVal.length; //IE WON'T BREAK HERE
like image 69
Shadow Wizard Hates Omicron Avatar answered Nov 01 '22 16:11

Shadow Wizard Hates Omicron