-
Notifications
You must be signed in to change notification settings - Fork 0
/
js--q1-3--distance-unit-converter.html
69 lines (64 loc) · 1.99 KB
/
js--q1-3--distance-unit-converter.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<!--
Q1.3 Create a distance convertor in HTML that will convert meters in K.Ms.
Hint: Take an input from textbox and print result in another textbox.
Formula: 1KM=1000 M
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>JS Task 1.3</title>
<link rel="stylesheet" href="../../common-style.css" />
<style>
select:focus option:first-child {
display: none;
}
label * {
margin-block: 10px;
}
</style>
</head>
<body>
<form action="#">
<label for="unit-converter"
>Convert distance measurement units<br />
<select name="unit-converter" id="convertTo">
<option value="#">Convert to</option>
<option value="km">Metre to KM</option>
<option value="mtr">KM to Metre</option>
</select>
<input
type="number"
name="unit-converter"
placeholder="Enter a value.."
id=""
/>
</label>
<label for="result"
>Result
<p id="result" name="result"></p>
</label>
<button type="button" value="convert">Convert</button>
</form>
<script>
// target button
const convertBtn = document.querySelector("button");
convertBtn.addEventListener("click", function () {
// target and get the selected choice
const selectedChoice = document.querySelector("#convertTo").value;
// target input field and get the value
const inputValue = document.querySelector("input").value;
// target result field
const resultField = document.querySelector('#result');
// evaluate choice
if(selectedChoice == 'km'){
resultField.innerText = inputValue + "Mtr(s) = " + inputValue/1000 + "KM(s)";
}
else{
resultField.innerText = inputValue + "KM(s) = " + inputValue*1000 + "Mtr(s)";
}
});
</script>
</body>
</html>