O'Reilly logo

Python 3 Web Development Beginner's Guide by Michel Anders

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Time for action conversion using unitconverter.js

unitconverter.js contains the necessary JavaScript code to do the actual conversion. It starts with the definition of a conversion map, a dictionary holding the conversion factors for any conversion we want to define. We restrict ourselves to conversions from inches to centimeters and vice versa, but additional conversion factors can easily be added.

conversion_map = {
	"inch cm":1.0/2.54,
	"cm inch":2.54
};
$("button").button().click(function(){ value=$("input[name='from']").val(); f=$("select[name='tounit'] option:selected").val(); t=$("select[name='fromunit'] option:selected").val(); if(f != t){ c=conversion_map[f+' '+t]; result=parseFloat(value)*c; }else{ result = value; } $("input[name='to']").val(result); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required