-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpractice.js
92 lines (68 loc) · 2.97 KB
/
practice.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
let names = [];
let showFormBtn = document.createElement("button");
showFormBtn.innerText = "فرم را نمایش بده";
showFormBtn.addEventListener("click", function () {
let formDiv = document.createElement("div");
let form = document.createElement("form");
let fullNameInput = document.createElement("input");
fullNameInput.setAttribute("type", "text");
fullNameInput.setAttribute("name", "FullName");
fullNameInput.setAttribute("placeholder", "Full Name");
let passwordInput = document.createElement("input");
passwordInput.setAttribute("type", "password");
passwordInput.setAttribute("name", "password");
passwordInput.setAttribute("placeholder", "Password");
let submitBtn = document.createElement("input");
submitBtn.setAttribute("type", "button");
submitBtn.setAttribute("value", "button");
form.appendChild(fullNameInput);
form.appendChild(passwordInput);
form.appendChild(submitBtn);
formDiv.appendChild(form);
document.body.appendChild(formDiv);
submitBtn.addEventListener("click", function () {
let fullNameValue = fullNameInput.value;
let passwordValue = passwordInput.value;
let fullNameParagraph = document.createElement("p");
fullNameParagraph.innerText = "Full Name: " + fullNameValue;
formDiv.appendChild(fullNameParagraph);
let passwordParagraph = document.createElement("p");
passwordParagraph.innerText = "Password: " + passwordValue;
formDiv.appendChild(passwordParagraph);
names.push(fullNameValue);
fullNameInput.value = "";
passwordInput.value = "";
});
let searchInput = document.createElement("input");
searchInput.setAttribute("type", "text");
searchInput.setAttribute("placeholder", "نام را وارد کنید");
let searchButton = document.createElement("button");
searchButton.innerText = "جستجو";
searchButton.addEventListener("click", function () {
let searchValue = searchInput.value;
searchFullName(searchValue);
});
document.body.appendChild(searchInput);
document.body.appendChild(searchButton);
document.body.appendChild(form);
});
document.body.appendChild(showFormBtn);
function searchFullName(fullName) {
let searchResults = names.filter(function (name) {
return name.includes(fullName);
});
let searchResultsDiv = document.createElement("div");
searchResultsDiv.innerHTML = "<h3>نتایج جستجو:</h3>";
if (searchResults.length === 0) {
searchResultsDiv.innerHTML += "<p>نتیجهای یافت نشد.</p>";
} else {
let resultList = document.createElement("ul");
searchResults.forEach(function (result) {
let listItem = document.createElement("li");
listItem.innerText = result;
resultList.appendChild(listItem);
});
searchResultsDiv.appendChild(resultList);
}
document.body.appendChild(searchResultsDiv);
}