|
3 | 3 | <head>
|
4 | 4 | <meta charset="utf-8">
|
5 | 5 | <script>
|
6 |
| - // TODO: create a handler |
| 6 | + let actions = { |
| 7 | + google: "https://www.google.com/search", |
| 8 | + duckDuckGo: "https://duckduckgo.com/", |
| 9 | + bing: "https://www.bing.com/search", |
| 10 | + ask: "https://www.ask.com/web" |
| 11 | + } |
| 12 | + function setSearchEngine() { |
| 13 | + const buttonChosen = document.querySelector("input[name=engine]:checked"); |
| 14 | + let input = document.querySelector("input[name=q]"); |
| 15 | + if (!buttonChosen){ |
| 16 | + alert("Choose a button!") |
| 17 | + }; |
| 18 | + if (input.value === ""){ |
| 19 | + alert("Please enter a query!") |
| 20 | + }; |
| 21 | + searchForm.action = actions[buttonChosen.value] |
| 22 | + }; |
| 23 | + |
7 | 24 | window.addEventListener("load", function(){
|
8 |
| - // TODO: register the handler |
9 |
| - }); |
| 25 | + const form = document.getElementById("searchForm"); |
| 26 | + form.addEventListener('submit', setSearchEngine); |
| 27 | + }); |
| 28 | + |
10 | 29 | </script>
|
11 | 30 | </head>
|
12 | 31 |
|
13 | 32 | <body>
|
14 | 33 |
|
15 |
| - <form id="searchForm"> |
16 |
| - <!-- TODO: add form elements --> |
| 34 | + <form id="searchForm" target_blank > |
| 35 | + <input type = "text" name ="q" /> |
| 36 | + <label> <input type = "radio" name ="engine" value = "google" </> Google</label> |
| 37 | + <label> <input type = "radio" name ="engine" value = "duckDuckGo" </> DuckDuckGo</label> |
| 38 | + <label> <input type = "radio" name ="engine" value = "bing" </> Bing</label> |
| 39 | + <label> <input type = "radio" name ="engine" value = "ask" </> Ask</label> |
| 40 | + <button id="submit" value = "Go!"> Go! </button> |
17 | 41 | </form>
|
18 | 42 |
|
19 | 43 | </body>
|
0 commit comments