Skip to content

Instantly share code, notes, and snippets.

@jonathansampson
Created August 4, 2014 15:48
Show Gist options
  • Save jonathansampson/c9071cc90e0014f4859f to your computer and use it in GitHub Desktop.
Save jonathansampson/c9071cc90e0014f4859f to your computer and use it in GitHub Desktop.
`valueAsNumber` shim for Internet Explorer 11
(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 ); }
});
}
}
}());
@comster
Copy link

comster commented Mar 2, 2015

WTF is wrong with IE? OMG!!!

Thank you for your service to humanity good sir!

@comster
Copy link

comster commented Mar 2, 2015

IE 10 as well btw

@tomtheisen
Copy link

I'd suggest using parseFloat() if you need to deal with non-integers.

@lumpypuppy
Copy link

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