Jump to content
  • Advertisement
Sign in to follow this  
sooner123

How to make Enter key do the same thing as submit button?

This topic is 3079 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

<form> <table> <tr> <td>name:</td><td><input type="text" id="txt1" size=10></td> </tr> <tr> <td></td><td><input type="button" value="submit" onclick="button_1()"></td> </tr> </table> </form> The form calls the button_1() function when I click submit. How do I make it so it also calls this function if the user presses enter while the focus is in the first text field?

Share this post


Link to post
Share on other sites
Advertisement
I assume the question is more related to performing some JavaScript action when submitting the form, not specifically handling pressing enter in a text field. You can do this with the form's onsubmit event. In your case you should probably also change your button into a submit rather than button input field.

<form onsubmit="return button_1();">
<table>
<tr>
<td>name:</td><td><input type="text" id="txt1" size="10"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="submit"></td>
</tr>
</table>
</form>

Note that if you use "return some_value;" in an onsomething handler you can prevent it from performing its action (in this case, returning false from button_1() would prevent the form from being submitted normally, but returning true would let it submit as normal).

Share this post


Link to post
Share on other sites
That didn't work. It made it so when I hit enter in the text box, the error message "bad login info" appears briefly then disapears. It's almost like it's doing some rapid page load or something?


<html>
<head>
<script type="text/javascript">

function button_1()
{
url = "process_login.php?name=" + document.getElementById('txt1').value;
xmlhttp = GetXmlHttpObject();
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET", url, true);
xmlhttp.send(null);
}

function stateChanged()
{
if (xmlhttp.readyState==4)
{
if(xmlhttp.responseText)
{
//here goes the redirect code
window.location='success.php';
}
else document.getElementById("result").innerHTML="bad login info";
}
}

function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
return new XMLHttpRequest();
}

if (window.ActiveXObject)
{
// code for IE6, IE5
return new ActiveXObject("Microsoft.XMLHTTP");
}

return null;
}

</script>
</head>

<body>
<form onsubmit="button_1()">
<table>
<tr>
<td>name:</td><td><input type="text" id="txt1" size=10></td>
</tr>
<tr>
<td></td><td><input type="button" value="submit" onclick="button_1()"></td>
</tr>
</table>
</form>
<div id = "result"></div>
</body>
</html>

Share this post


Link to post
Share on other sites
You need to return false from your onsubmit handler, to prevent the browser from submitting the form as it normally would do.

Share this post


Link to post
Share on other sites
By the gods, that did it. I added 'return false;' to the end of function button_1(), and added an 'onsubmit = "return button_1()"' attribute to my form tag. I do find it somewhat strange that i had to return it in the onsubmit instead of just calling it. I guess I'm not 100% clear on how function calls from an html tag really work. May Allah bless your family for a hundred generations and smite your enemies.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!