Created
August 4, 2014 15:48
-
-
Save jonathansampson/c9071cc90e0014f4859f to your computer and use it in GitHub Desktop.
`valueAsNumber` shim for Internet Explorer 11
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function () { | |
/* Internet Explorer 11 may have trouble retrieving the number type | |
of an input value. This short script performs a quick test, and repairs | |
the functionality if necessary. Load before attempting to use the | |
`valueAsNumber` property on input elements. */ | |
"use strict"; | |
var a = document.createElement( "input" ); | |
a.setAttribute( "type", "number" ); | |
a.setAttribute( "value", 2319 ); | |
if ( "valueAsNumber" in a && a.value != a.valueAsNumber ) { | |
if ( "defineProperty" in Object && "getPrototypeOf" in Object ) { | |
Object.defineProperty( Object.getPrototypeOf( a ), "valueAsNumber", { | |
get: function () { return parseInt( this.value, 10 ); } | |
}); | |
} | |
} | |
}()); |
I'd suggest using parseFloat()
if you need to deal with non-integers.
This works great. Now I will go to the local court house and legally change all my kids' names to 'Jonathan Sampson.' You rock.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
IE 10 as well btw