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

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!

This question is related to javascript html forms submit

The answer is


<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.


Looks like your form is submitting which is the default behaviour, you can stop it with this:

<form action="" method="post" onsubmit="completeAndRedirect();return false;">

You need to prevent the default behaviour. You can either use e.preventDefault() or return false; In this case, the best thing is, you can use return false; here:

<form onsubmit="completeAndRedirect(); return false;">

Examples related to javascript

need to add a class to an element How to make a variable accessible outside a function? Hide Signs that Meteor.js was Used How to create a showdown.js markdown extension Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Summing radio input values How to execute an action before close metro app WinJS javascript, for loop defines a dynamic variable name Getting all files in directory with ajax

Examples related to html

Embed ruby within URL : Middleman Blog Please help me convert this script to a simple image slider Generating a list of pages (not posts) without the index file Why there is this "clear" class before footer? Is it possible to change the content HTML5 alert messages? Getting all files in directory with ajax DevTools failed to load SourceMap: Could not load content for chrome-extension How to set width of mat-table column in angular? How to open a link in new tab using angular? ERROR Error: Uncaught (in promise), Cannot match any routes. URL Segment

Examples related to forms

How do I hide the PHP explode delimiter from submitted form results? React - clearing an input value after form submit How to prevent page from reloading after form submit - JQuery Input type number "only numeric value" validation Redirecting to a page after submitting form in HTML Clearing input in vuejs form Cleanest way to reset forms Reactjs - Form input validation No value accessor for form control TypeScript-'s Angular Framework Error - "There is no directive with exportAs set to ngForm"

Examples related to submit

React - clearing an input value after form submit angular2 submit form by pressing enter without submit button spark submit add multiple jars in classpath jQuery's .on() method combined with the submit event Selenium Webdriver submit() vs click() PHP form - on submit stay on same page Disable submit button ONLY after submit HTML - How to do a Confirmation popup to a Submit button and then send the request? HTML form with multiple "actions" Javascript change color of text and background to input value