Search code examples
javascriptgoogle-gadget

Press "enter" to submit form doesn't work (js)


i'm using google gadget to write a short script and try to insert to google site. When user press Go button, javascript function is triggered and everything works fine, but when user press Enter, error occurs:

Missing or malformed url parameter

I'm not sure it's caused by my code or google gadget/site.

My html code:

<form name="theform">
<input type="text" size="20" id="search"/> 
<INPUT TYPE="submit" VALUE="Go!" ONCLICK="GotoURL(this.form)">
</form>

javascript function

function GotoURL(dl) { 

var mySearch = document.getElementById("search").value;
url='some_url'+mySearch;
window.open(url,'_blank');
} 

Thanks


Solution

  • You should use the onsubmit event on the form instead of the onclick event of the button:

    <form name="theform" onsubmit="GotoURL(this)">
        <input type="text" size="20" id="search"/> 
        <input type="submit" value="Go!">
    </form>
    

    Make sure that the GotoURL() function returns false to prevent the form from actually submitting.