|
4 | 4 | <meta charset="utf-8"> |
5 | 5 | <script> |
6 | 6 | // 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 | + } |
7 | 27 | window.addEventListener("load", function(){ |
8 | 28 | // TODO: register the handler |
9 | | - }); |
| 29 | + const form = document.getElementById("searchForm"); |
| 30 | + form.addEventListener("submit",setSearchEngine); |
| 31 | + }); |
10 | 32 | </script> |
11 | 33 | </head> |
12 | 34 |
|
13 | 35 | <body> |
14 | 36 |
|
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> |
17 | 44 | </form> |
18 | 45 |
|
19 | 46 | </body> |
0 commit comments