Here's a simple demo of changing a style rule. There are much more sophisticated ways of going about it, but this is a simple method for this application. By default you could show either one or both units.
The rules to be changed must exist in an existing style sheet.
<style type="text/css">
.metric {
display: inline;
}
.imperial {
display: none;
}
</style>
<script>
// Search style sheets for selector. If found, set
// prop to value. Stops when first selector found
function setRuleValue(selector, prop, value) {
selector = selector.toLowerCase();
// Get the style sheets
var sheet, sheets = document.styleSheets;
var j, rule, rules;
if (sheets) {
// Search style sheets for rule
for (var i=0, iLen=sheets.length; i<iLen; i++) {
sheet = sheets[i];
j = 0;
// Choose .cssRules or .rules, whichever is supported
while (rule = (sheet.cssRules[j] || sheet.rules[j])) {
j++;
if (rule.selectorText.toLowerCase() == selector) {
rule.style[prop] = value;
// If had success, return true
return true;
}
}
}
}
// If get here, didn't find rule so return false
return false;
}
// Turn metric on, imperial off
function showMetric() {
setRuleValue('.metric', 'display', '');
setRuleValue('.imperial', 'display', 'none' );
}
// Turn imperial on, metric off
function showImperial() {
setRuleValue('.metric', 'display', 'none');
setRuleValue('.imperial', 'display', '' );
}
</script>
<div>This part is <span class="imperial">3 inches</span><span class="metric">25.4 mm</span> long</div>
<br>
<button onclick="showMetric()">Show metric</button>
<button onclick="showImperial()">Show imperial</button>
If you have a table, you can create a colgroup for each of metric and imperial, then just change the which colgroup is hidden or shown. By default, show both.