-
Notifications
You must be signed in to change notification settings - Fork 0
/
clickToAddItems.js
51 lines (48 loc) · 2.02 KB
/
clickToAddItems.js
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
function clearErrors() {
for (var loopCounter = 0;
loopCounter < document.forms["numberFun"].elements.length;
loopCounter++) {
if (document.forms["numberFun"].elements[loopCounter]
.parentElement.className.indexOf("has-") != -1) {
document.forms["numberFun"].elements[loopCounter]
.parentElement.className = "form-group";
}
}
}
function resetForm() {
clearErrors();
document.forms["numberFun"]["num1"].value = "";
document.forms["numberFun"]["num2"].value = "";
document.getElementById("results").style.display = "none";
document.getElementById("submitButton").innerText = "Submit";
document.forms["numberFun"]["num1"].focus();
}
function validateItems() {
clearErrors();
var num1 = document.forms["numberFun"]["num1"].value;
var num2 = document.forms["numberFun"]["num2"].value;
if (num1 == "" || isNaN(num1)) {
alert("Num1 must be filled in with a number.");
document.forms["numberFun"]["num1"]
.parentElement.className = "form-group has-error";
document.forms["numberFun"]["num1"].focus();
return false;
}
if (num2 == "" || isNaN(num2)) {
alert("Num2 must be filled in with a number.");
document.forms["numberFun"]["num2"]
.parentElement.className = "form-group has-error"
document.forms["numberFun"]["num2"].focus();
return false;
}
document.getElementById("results").style.display = "block";
document.getElementById("submitButton").innerText = "Recalculate";
document.getElementById("addResult").innerText = Number(num1) +
Number(num2);
document.getElementById("subtractResult").innerText = num1 - num2;
document.getElementById("multiplyResult").innerText = num1 * num2;
document.getElementById("divideResult").innerText = num1 / num2;
// We are returning false so that the form doesn't submit
// and so that we can see the results
return false;
}