0

THis is the code :

<script>
    function searchFor(searchEngine,searchQuery){
        if (searchEngine="google"){
            var x="http://google.com.hk/search?q="+searchQuery;
            window.location=x
        }

        if (searchEngine="yahoo"){
            var x="http://hk.search.yahoo.com/search?p="+searchQuery;
            window.location=x
        }
     }
</script>

<form>
     <input type="radio" name="searchLoc" value="google"> Google <input type="radio" name="searchLoc" value="yahoo"> Yahoo
     <br>
     <input type="text" name="searchContent">
     <input type="submit" onclick="searchFor(this.form.searchLoc.value,this.form.searchContent.value);return false;" value="Search"
</form>

Why is it not working?

Once i click the submit button, nothing happened.

Pls help. Thx.

4 Answers 4

2

You should write

<script>
function searchFor(searchEngine,searchQuery){
if (searchEngine=="google"){
var x="http://google.com.hk/search?q="+searchQuery;
window.location=x
}
if (searchEngine=="yahoo"){
var x="http://hk.search.yahoo.com/search?p="+searchQuery;
window.location=x
}
}
</script>

'==' instead of '=' in comparison

Change submit button also,

<input type="submit" onclick="searchFor(this.form.searchLoc.value,this.form.searchContent.value);return false;" value="Search" />
Sign up to request clarification or add additional context in comments.

Comments

1

Certain error

a) it should be window.location.href

b) close the submit button tag

c) use '==' equal to for comparision

  <script>
    function searchFor(searchEngine,searchQuery){

     for (var i = 0; i < searchEngine.length; i++) {
        if (searchEngine[i].type === 'radio' && searchEngine[i].checked) {

            value = searchEngine[i].value;       
        }
    }


        if (value=="google"){
        var x="http://google.com.hk/search?q="+searchQuery;
        window.location.href=x
        }
        if (value=="yahoo"){
        var x="http://hk.search.yahoo.com/search?p="+searchQuery;
        window.location.href=x
        }
    }
    </script>

    <form>
    <input type="radio" name="searchLoc" value="google"> Google <input type="radio" name="searchLoc" value="yahoo"> Yahoo
    <br><input type="text" name="searchContent">
    <input type="submit" onclick="searchFor(this.form.searchLoc,this.form.searchContent.value);return false;" value="Search" />
</form>

Comments

0
<script type="text/javascript">
function searchFor(searchEngine,searchQuery){
    if (searchEngine=="google"){
        var x="http://google.com.hk/search?q="+searchQuery;
        window.location=x
    }
    if (searchEngine=="yahoo"){
        var x="http://hk.search.yahoo.com/search?p="+searchQuery;
        window.location=x;
    }
}
</script>

<form onsubmit="searchFor(this.form.searchLoc.value,this.form.searchContent.value);return false;">
<input type="radio" name="searchLoc" value="google"> Google <input type="radio" name="searchLoc" value="yahoo"> Yahoo
<br><input type="text" name="searchContent">
<input type="submit" value="Search"/>
</form>

Modify the code as above. searchEngine="google" should be searchEngine == "google" and use onsubmit on tag instead of onclick on submit button.

Comments

0

instead of:

  if (searchEngine="google")

try this:

if (searchEngine === "google")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.