-
Notifications
You must be signed in to change notification settings - Fork 0
/
form js.html
44 lines (37 loc) · 1.46 KB
/
form js.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
<html>
<head>
<title>Array Difference</title>
</head>
<body>
<form id="myForm">
<label for="arr1">Array 1:</label>
<input type="text" id="arr1" name="arr1" placeholder="Enter array 1 (comma-separated values)"><br><br>
<label for="arr2">Array 2:</label>
<input type="text" id="arr2" name="arr2" placeholder="Enter array 2 (comma-separated values)"><br><br>
<input type="submit" value="Get Difference">
</form>
<div id="result"></div>
<script>
// Get the form elements
const form = document.getElementById("myForm");
const arr1Input = document.getElementById("arr1");
const arr2Input = document.getElementById("arr2");
const resultDiv = document.getElementById("result");
// Add an event listener to the form submit event
form.addEventListener("submit", (e) => {
e.preventDefault();
// Get the input values from the form
const arr1 = arr1Input.value.split(",");
const arr2 = arr2Input.value.split(",");
// Call the function to get the difference
const result = getDifference(arr1, arr2);
// Display the result
resultDiv.innerText = `Result: ${result.join(", ")}`;
});
// Function to get the difference between two arrays
function getDifference(arr1, arr2) {
return arr1.filter((element) => !arr2.includes(element));
}
</script>
</body>
</html>