Here’s what I tried :
<input id="query" type="text" maxlength="32" size="50" placeholder="Search using GoogGoog..."></input>
<button id="search">Go</button>
<br>
<script type="text/javascript">
document.getElementById("search").onclick = function () {
if document.getElementById("query") == "chimic" {
location.href = "https://kornls.github.io/googgoog/dev/chimic.html";
} else {
location.href = "https://kornls.github.io/googgoog/dev/search.html";
};
</script>
But the button won’t redirect to any of the pages when clicked. What should I do?
Any help is appreciated. Thanks in advance!
>Solution :
It’s because of incorrect syntax.
Firstly, ( should come after the if and before document and at the end after "chimic" and before {
i.e. if (document.getElementById("query") == "chimic") {
Secondly, function block is opened with { but not ended. The } before the </script> should NOT have a semicolon, and this brace is ending the else block, not the function block.
<script type="text/javascript">
document.getElementById("search").onclick = function () {
if (document.getElementById("query") == "chimic") {
location.href = "https://kornls.github.io/googgoog/dev/chimic.html";
} else {
location.href = "https://kornls.github.io/googgoog/dev/search.html";
}
}
</script>