Weight unit converter

<!DOCTYPE html>

<html>

<head>

<title>Weight Conversions Calculator</title>

</head>

<body>

<h1>Weight Conversions Calculator</h1>

<form>

<label for=”input-weight”>Enter weight:</label>

<input type=”number” id=”input-weight” name=”input-weight”><br><br>

<label for=”select-from”>From:</label>

<select id=”select-from” name=”select-from”>

<option value=”kg”>Kilograms (kg)</option>

<option value=”lbs”>Pounds (lbs)</option>

<option value=”oz”>Ounces (oz)</option>

<option value=”g”>Grams (g)</option>

</select><br><br>

<label for=”select-to”>To:</label>

<select id=”select-to” name=”select-to”>

<option value=”kg”>Kilograms (kg)</option>

<option value=”lbs”>Pounds (lbs)</option>

<option value=”oz”>Ounces (oz)</option>

<option value=”g”>Grams (g)</option>

</select><br><br>

<input type=”button” value=”Convert” onclick=”convertWeight()”><br><br>

<label for=”output-weight”>Converted weight:</label>

<input type=”number” id=”output-weight” name=”output-weight” disabled>

</form>

 

<script>

function convertWeight() {

// Get input weight value

const inputWeight = parseFloat(document.getElementById(‘input-weight’).value);

 

// Get selected units for conversion

const fromUnit = document.getElementById(‘select-from’).value;

const toUnit = document.getElementById(‘select-to’).value;

 

// Calculate conversion factor

let factor = 1;

if (fromUnit === ‘kg’ && toUnit === ‘lbs’) {

factor = 2.20462;

} else if (fromUnit === ‘lbs’ && toUnit === ‘kg’) {

factor = 0.453592;

} else if (fromUnit === ‘kg’ && toUnit === ‘oz’) {

factor = 35.274;

} else if (fromUnit === ‘oz’ && toUnit === ‘kg’) {

factor = 0.0283495;

} else if (fromUnit === ‘lbs’ && toUnit === ‘oz’) {

factor = 16;

} else if (fromUnit === ‘oz’ && toUnit === ‘lbs’) {

factor = 0.0625;

} else if (fromUnit === ‘g’ && toUnit === ‘kg’) {

factor = 0.001;

} else if (fromUnit === ‘kg’ && toUnit === ‘g’) {

factor = 1000;

} else if (fromUnit === ‘g’ && toUnit === ‘lbs’) {

factor = 0.00220462;

} else if (fromUnit === ‘lbs’ && toUnit === ‘g’) {

factor = 453.592;

} else if (fromUnit === ‘g’ && toUnit === ‘oz’) {

factor = 0.035274;

} else if (fromUnit === ‘oz’ && toUnit === ‘g’) {

factor = 28.3495;

}

 

// Calculate converted weight

const

outputWeight = inputWeight * factor;

 

// Set output weight

Sharing is caring!