ZeroField
// zeroField helper function
function zeroField(el, decimal) {
var def_val;
if(!el) {
return false;
} else {
if(decimal == true) {
def_val = '0.00';
} else {
def_val = '0';
}
if(el.value == "" || el.value == null) {el.value = def_val;}
el.onfocus = function() { if (this.value==def_val) this.value = ''; };
el.onblur = function() { if (this.value=='') this.value = def_val; };
}
}
// Demonstration code
function init() {
var myTextfield = document.getElementById('myTextfield');
zeroField(myTextfield); // Will set the value of myTextfield to '0'
}
function init() {
var myTextfield = document.getElementById('myTextfield');
zeroField(myTextfield, true); // Will set the value of myTextfield to '0.00'
}
window.onload = init;
function zeroField(el, decimal) {
var def_val;
if(!el) {
return false;
} else {
if(decimal == true) {
def_val = '0.00';
} else {
def_val = '0';
}
if(el.value == "" || el.value == null) {el.value = def_val;}
el.onfocus = function() { if (this.value==def_val) this.value = ''; };
el.onblur = function() { if (this.value=='') this.value = def_val; };
}
}
// Demonstration code
function init() {
var myTextfield = document.getElementById('myTextfield');
zeroField(myTextfield); // Will set the value of myTextfield to '0'
}
function init() {
var myTextfield = document.getElementById('myTextfield');
zeroField(myTextfield, true); // Will set the value of myTextfield to '0.00'
}
window.onload = init;






There are currently no comments for this snippet.