Skip to content

Instantly share code, notes, and snippets.

@mattneary
Created July 20, 2013 18:51
Show Gist options
  • Save mattneary/6046057 to your computer and use it in GitHub Desktop.
Save mattneary/6046057 to your computer and use it in GitHub Desktop.
SO Adder
<script>
var inputs = {};
function save(value, name) {
inputs[name] = parseInt(value);
}
function checker(criterion, action) {
return function(value, name) {
if( !criterion(value) ) {
alert("Not Numeric");
} else {
action(value, name);
}
};
}
function isNumber(x) {
return !isNaN(x);
}
function addFunction(elm) {
elm.value = inputs.num1 + inputs.num2;
}
</script>
<input id="num1" type="text" onblur="checker(isNumber, save)(this.value, 'num1')">+
<input id="num2" type="text" onblur="checker(isNumber, save)(this.value, 'num2')"> = <input id="adds" type="text" onfocus="addFunction(this)">
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment