ZeroField





10
Date Submitted Wed. Nov. 9th, 2005 11:45 PM
Revision 1 of 1
Helper poncho
Tags Form | JavaScript | Zero
Comments 0 comments
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;

Mark Thompson

www.defaultstate.com/
Perfecting the art of breaking stuff!

Comments

There are currently no comments for this snippet.

Voting