Skip to content

Commit fe47e54

Browse files
committed
formsStudio
1 parent e9de0bc commit fe47e54

1 file changed

Lines changed: 30 additions & 3 deletions

File tree

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

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,43 @@
44
<meta charset="utf-8">
55
<script>
66
// TODO: create a handler
7+
function setSearchEngine(event) {
8+
event.preventDefault();
9+
10+
let actions = {
11+
"google": "https://www.google.com/search",
12+
"duckDuckGo": "https://duckduckgo.com/",
13+
"bing": "https://www.bing.com/search",
14+
"ask": "https://www.ask.com/web"
15+
};
16+
17+
const selectedEngine = document.querySelector("input[name=engine]:checked");
18+
19+
if (selectedEngine) {
20+
const engineValue = selectedEngine.value;
21+
const actionUrl = actions[engineValue];
22+
const form = document.getElementById("searchForm");
23+
form.setAttribute("action", actionUrl);
24+
form.submit();
25+
}
26+
}
727
window.addEventListener("load", function(){
828
// TODO: register the handler
9-
});
29+
const form = document.getElementById("searchForm");
30+
form.addEventListener("submit",setSearchEngine);
31+
});
1032
</script>
1133
</head>
1234

1335
<body>
1436

15-
<form id="searchForm">
16-
<!-- TODO: add form elements -->
37+
<form id="searchForm"><input type="text" name="q">
38+
<label>Google <input type="radio" name="engine" value="google"/></label>
39+
<label>DuckDuckGo <input type="radio" name="engine" value="duckDuckGo"/></label>
40+
<label>Bing <input type="radio" name="engine" value="bing"/></label>
41+
<label>Ask <input type="radio" name="engine" value="ask"/></label>
42+
43+
<button type="submit">Go!</button>
1744
</form>
1845

1946
</body>

0 commit comments

Comments
 (0)