How do I get the value of an <input>
tag with the type number
? This sounds like a ridiculous question, but in a recent1 Chrome update, the following code doesn't work if an invalid character is entered into the field:
element.value //does not work as expected, will return ""
Demo: http://jsfiddle.net/DerekL/zT4bf/
Now this is frustrating. I am using this piece of code to forbidden invalid characters:
$("element").on("input", function(){
this.value = this.value.replace(/[^0-9]/g, "");
});
But since it returns ""
now if an invalid character is entered, the whole field would be emptied. This behavior did not happen before, just in the recent Chrome update. This works fine in Firefox (although I do not need this to work in Firefox.)
Is there a way to get the exact value of the <input>
? Thanks.
1: 36.0.1985.49 beta-m