How to get the jQuery MaskedInput unmask() function to work properly?

I run through the same problem, and what helped me is:

    var maskedInput = $("#id").data('mask');
    if (maskedInput) {
        maskedInput.remove();
    }

You don't need to pass a parameter to the unmask function! Use it without a parameter and it works well.

$("#OfficePhone").unmask();

I was able to figure out the code to get it to work. Below is the code I am using now.

<script type="text/javascript">
$(function($) {
  $("#OfficePhone").mask("(999) 999-9999? x99999");
  $("#InternationalOfficePhone").click(function() {
    var mask = "(999) 999-9999? x99999";
    if ($("#InternationalOfficePhone").is(':checked')) {
      $("#OfficePhone").unmask(mask);
    } else {
      $("#OfficePhone").mask(mask);
    }
  });
});
</script>