-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAssignment-ListOfNames - Fancy.js
56 lines (49 loc) · 1.69 KB
/
Assignment-ListOfNames - Fancy.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
52
53
54
55
56
var firstNameTag = document.getElementById("FirstName");
var lastNameTag = document.getElementById("LastName");
var titleTag = document.getElementById("Title");
var namesListTag = document.getElementById("NamesList");
var nameArray = [];
function SaveName() {
var fullName = firstNameTag.value.trim() + " " + lastNameTag.value.trim();
if (fullName.length < 4) {
alert("Please enter a name.");
return;
}
nameArray.push(fullName);
ClearTextBoxes();
alert(fullName + " was saved!");
}
function ShowNamesCollected() {
namesListTag.innerHTML = "";
if (nameArray.length === 0) {
titleTag.innerHTML = "No Names Have Been Collected";
} else {
titleTag.innerHTML = nameArray.length + " Name";
if (nameArray.length > 1) titleTag.innerHTML = titleTag.innerHTML + "s";
titleTag.innerHTML = titleTag.innerHTML + " Collected:";
for (var i = 0; i < nameArray.length; i++) {
namesListTag.innerHTML = namesListTag.innerHTML + "<li>" + nameArray[i] + "</li>";
}
}
}
function ClearTextBoxes() {
firstNameTag.value = "";
lastNameTag.value = "";
firstNameTag.focus();
}
function ResetForm() {
nameArray = [];
ClearTextBoxes();
titleTag.innerHTML = "";
namesListTag.innerHTML = "";
}
function ListenForEnterKey() {
document.addEventListener("keyup", function(event) {
// Number 13 is the "Enter" key on the keyboard
if (event.keyCode === 13) {
event.preventDefault(); //Cancel the default action of the Enter key
document.getElementById("SaveButton").click();
}
});
firstNameTag.focus();
}