OakvilleWork
OakvilleWork

Reputation: 2377

script not working in IE 9

i have this script for the check of a checkbox, it works well in firefox and google chrome, however it does not work in IE 9

    $notifyBySmsCheckbox
  .change(function(){
    if (this.checked) {console.log('checked');
      $('#f_notify_by_sms_instructions, #f_notify_by_sms_pin, #cellphoneInfo').show();
    }
    else {console.log('not checked');
      $('#f_notify_by_sms_instructions, #f_notify_by_sms_pin, #cellphoneInfo').hide();
    }
  })
  .change();

anyone know why this is??

Upvotes: 1

Views: 645

Answers (3)

João Silva
João Silva

Reputation: 91319

Only access console when it's defined:

  $notifyBySmsCheckbox.change(function() {   
    if (this.checked) {
      console && console.log('checked');
      $('#f_notify_by_sms_instructions, #f_notify_by_sms_pin, #cellphoneInfo').show();
    } else {
      console && console.log('not checked');
      $('#f_notify_by_sms_instructions, #f_notify_by_sms_pin, #cellphoneInfo').hide();
    }
  })

Also, you can use toggle to simplify your code, as suggested by @epascarello:

  $notifyBySmsCheckbox.change(function() {
    console && console.log(this.checked ? "checked" : "not checked");
    $('#f_notify_by_sms_instructions, #f_notify_by_sms_pin, #cellphoneInfo').toggle(this.checked);
  });

Upvotes: 3

vector
vector

Reputation: 7576

... it will work if you have dev tools open in ie, otherwise it won't. Do you have to support ie 8 and 7? - if so you'll have problems with the onchange event in these. Use onClick instead.

Upvotes: 1

Chris Beemster
Chris Beemster

Reputation: 362

..it's most likely because IE doesn't have a console object. Try and remove the console.log's and you'll be fine!

Upvotes: 5

Related Questions