Попробуйте так:
document.addEventListener('DOMContentLoaded', function() {
var dir,
sqr = '²',
mul = {
'01': 100,
'12': 10,
'02': 1000,
'10': .01,
'21': .1,
'20': .001
},
dim = [{
unt: ' м',
dec: 2,
mul: 1
}, {
unt: ' см',
dec: 0,
mul: .0001
}, {
unt: ' мм',
dec: 0,
mul: .000001
}],
side = [].slice.call(document.querySelectorAll('input[type="text"]'));
side.forEach(function(node) {
node.addEventListener('keyup', function() {
this.value = this.value.replace(/[,\.]+/, '.').replace(/[^\d.]/, '');
var u = unit.filter(function(node) {
return !!node.checked;
}).shift().value || '';
[].forEach.call(document.querySelectorAll('[data-area]'), function(node) {
var group = node.dataset.area,
values = side.filter(function(node) {
return node.classList.contains(group);
}).map(function(e) {
return parseFloat(e.value) || 0;
}),
area = calculate(group, values);
[].forEach.call(node.querySelectorAll('.area'), function(e, i) {
e.textContent = area[i] ? (area[i] * dim[u].mul).toFixed(2) + ' м' + sqr : '';
});
})
});
});
var unit = [].slice.call(document.querySelectorAll('input[type="radio"]'));
unit.forEach(function(node) {
node.addEventListener('change', function() {
var u = this.value,
d = dir + u;
[].forEach.call(document.querySelectorAll('.unit'), function(node) {
node.textContent = dim[u].unt;
});
side.forEach(function(node) {
var v = parseFloat(node.value);
if (v)
node.value = (v * mul[d]).toFixed(dim[u].dec);
});
side.forEach(function(node) {
node.dispatchEvent(new Event('keyup'));
});
});
node.parentElement.addEventListener('mousedown', function() {
dir = unit.filter(function(node) {
return !!node.checked;
}).shift().value || '';
});
});
});
if (document.readyState === 'complete')
document.dispatchEvent(new Event('DOMContentLoaded'));