Unit Conversion Demo

Inch-Centimeter Converter

Description of example

Code:

<script>
var inf, cmf;

//called on page load, we get and store the elements that hold the inches and cms
function init(){   
	inf = document.getElementById('in');
    cmf = document.getElementById('cm');
}
//
function convert(){   
	var i = inf.value.replace(/ /,"");
    if ( i ){  
    	cmf.value = i * 2.54; 
       return;
    }
    //if there was nothing in the inches, check the cms
    var c = cmf.value.replace(/ /,"");
    if ( c ){  
    	inf.value = c / 2.54; 
    }
}
//empties both fields
function reset(){   
	inf.value = "";   
	cmf.value = "";   
}

</script>

Output: