Submit HTML form, perform javascript function (alert then redirect)

Harry picture Harry · Oct 30, 2012 · Viewed 115.7k times · Source

I have a JavaScript function that works great when I call it through an 'onClick' method. Basically what it does is pull data from a html form, and then based on that data, redirects to another page.

However, when I press the 'enter' button to submit the form/call the JavaScript function, instead of using a onClick button to call the JavaScript function, it doesn't respond as I would like it to.

I would like for when a user presses the enter button on their keyboard, the same thing happens as if they were to click the 'proceed button' on by page (which calls the function via onClick)

Here is my code:

The JS Function:

function completeAndRedirect(){
    alert('You\'re nearly there! To complete your entry, you simply need to tap \'continue\' on the next page. Good luck!');
    location.href='http://google.com/?SID='+'<? echo $CATEGORY; ?>'+','+'<? echo $PLATFORM; ?>'+','+'<? echo $DEVICE; ?>'+'&email='+document.forms[0].elements[0].value;
}

Here is the form:

<form action="" method="post" onsubmit="completeAndRedirect()">
    <input type="text" id="Edit1" style="width:280; height:50; font-family:'Lucida Sans Unicode', 'Lucida Grande', sans-serif; font-size:22px">
</form>

Huge thanks to anyone who can help out!

Answer

user1621465 picture user1621465 · Oct 30, 2012
<form action="javascript:completeAndRedirect();">
    <input type="text" id="Edit1" 
    style="width:280; height:50; font-family:'Lucida Sans Unicode', 'Lucida Grande', sans-serif; font-size:22px">
</form>

Changing action to point at your function would solve the problem, in a different way.