Skip to content

Commit d082a9f

Browse files
committed
forms studio
1 parent a910bdf commit d082a9f

File tree

1 file changed

+42
-6
lines changed

1 file changed

+42
-6
lines changed

user-input-with-forms/studio/index.html

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,53 @@
33
<head>
44
<meta charset="utf-8">
55
<script>
6-
// TODO: create a handler
7-
window.addEventListener("load", function(){
8-
// TODO: register the handler
9-
});
6+
function setSearchEngine() {
7+
8+
let actions = {
9+
"google": "https://www.google.com/search",
10+
"duckDuckGo": "https://duckduckgo.com/",
11+
"bing": "https://www.bing.com/search",
12+
"ask": "https://www.ask.com/web"
13+
};
14+
15+
let form = document.getElementById('searchForm');
16+
let engine = document.querySelector('input[name=engine]:checked');
17+
let action = actions[engine.value]
18+
form.setAttribute('action', action)
19+
20+
}
21+
window.addEventListener("load", function () {
22+
let form = document.getElementById('searchForm');
23+
form.addEventListener('submit', setSearchEngine);
24+
if (engine.value == "") {
25+
alert("Must enter a search term!");
26+
event.preventDefault();
27+
}
28+
});
1029
</script>
1130
</head>
1231

1332
<body>
1433

1534
<form id="searchForm">
16-
<!-- TODO: add form elements -->
35+
<input type="text" name="q" />
36+
<label>
37+
<input type="radio" name="engine" value="google">
38+
Google
39+
</label>
40+
<label>
41+
<input type="radio" name="engine" value="duckDuckGo">
42+
DuckDuckGo
43+
</label>
44+
<label>
45+
<input type="radio" name="engine" value="bing">
46+
Bing
47+
</label>
48+
<label>
49+
<input type="radio" name="engine" value="ask">
50+
Ask
51+
</label>
52+
<input type="submit" value="Go!">
1753
</form>
1854

19-
</body>
55+
</body>

0 commit comments

Comments
 (0)