forked from LaunchCodeEducation/HTTP-and-Forms-Studio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
50 lines (42 loc) · 1.39 KB
/
index.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
<!doctype html>
<head>
<meta charset="utf-8">
<script>
// TODO: create a handler
function setSearchEngine() {
let engineSelect = document.querySelector("input[name=engine]:checked").value.toString();
let actions = {
"google": "https://www.google.com/",
"duckduckgo": "https://duckduckgo.com/",
"bing": "https://www.bing.com/search",
"ask": "https://www.ask.com/web"
};
let url = actions[engineSelect];
return url;
}
window.addEventListener("load", function() {
let form = document.getElementById("searchForm");
form.addEventListener("submit", function(event) {
let searchInput = document.querySelector("input[name=q]");
let engineSelect = document.querySelector("input[name=engine]:checked");
if (searchInput.value === "" || engineSelect === null) {
alert("All fields are required!");
event.preventDefault();
} else {
let url = setSearchEngine();
form.setAttribute("action", url);
}
});
});
</script>
</head>
<body>
<form id="searchForm">
<label><input type="text" name="q"></label>
<label>Google<input type="radio" name="engine" value="google" /></label>
<label>DuckDuckGo<input type="radio" name="engine" value="duckduckgo" /></label>
<label>Bing<input type="radio" name="engine" value="bing" /></label>
<label>Ask<input type="radio" name="engine" value="ask" /></label>
<button>Go!</button>
</form>
</body>