I just wanted to make a proof of concept that based on the person’s search text and the option they select from the dropdown it will redirect them to the search engine of their choice.
JavaScript
x
37
37
1
const options = [
2
{ value: 'http://www.google.com/search?q=', label: 'Google' },
3
{ value: 'http://search.yahoo.com/search?p=', label: 'Yahoo' },
4
{ value: 'https://www.bing.com/search?q=', label: 'Bing' },
5
{ value: 'https://duckduckgo.com/?q=', label: 'DuckDuckGo' }
6
]
7
8
//const [selection, setSearch] = useState("");
9
10
11
const doSearch = event => {
12
event.preventDefault();
13
var sf=document.searchform;
14
var submitto = sf.sengines[sf.sengines.selectedIndex].value + (sf.searchterms.value);
15
console.log("log: " + submitto);
16
window.location.href = submitto;
17
//window.location.replace(submitto)
18
return null;
19
}
20
21
function App() {
22
return (
23
<div className="App">
24
<header className="App-header">
25
<img src={logo} className="App-logo" alt="logo" />
26
<p>Search:</p>
27
<form name="searchform" onSubmit={doSearch}>
28
<Select id="sengines" options={options}/>
29
For:
30
<input type="text" name='searchTerms'/>
31
<input type="submit" name="SearchSubmit" value="Search"></input>
32
</form>
33
</header>
34
</div>
35
);
36
}
37
When I hit search it throws an error saying that the selectedIndex is undefined. Is there a syntax mistake I am making that I am unaware of?
Advertisement
Answer
The problem was easier to solve than I realized.
JavaScript
1
2
1
sf.searchTerms.value
2
Needs to be camel cased