0

I'm wanting to format numbers based on keypress / ng-change. After reading this post, I though I would be able to use some of the sample code to format my numbers with a fixed precision. The code works fine when called on page load and written out to the browser, it does not work when I apply the same code to the value of a text field on key press.

function GetChar (event){
    var theValue = document.getElementById('foo').value;

      document.write(theValue.format(2, 3, '.', '.'));
}

Fiddle

Community
  • 1
  • 1
Jimi
  • 1,867
  • 7
  • 31
  • 55

1 Answers1

2

You have added a function to Number.prototype, but your variable theValue is a String.

You should convert the value to a Number. There are several ways to convert a String to a Number. One way is to use the Number() function, like this:

var theValue = Number(document.getElementById('foo').value);

You should also use onkeyup because the typed key will not yet be included in the value of the input element during the handling of onkeydown.

jsfiddle

Community
  • 1
  • 1
John S
  • 21,212
  • 8
  • 46
  • 56