Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Enter key not submitting form

Featured Replies

Hey all, I've got some simple JS that checks the value of a field (user input) and directs to a page based on the value. So theres an onclick event rather than a form action. Trouble is, now the enter key doesn't submit the form, it just reloads the page. Anyone know of a workaround for this? thanks guys :)

Can you show us some code?

If you're using a form, the onsubmit event is what you want to look for.

  • Author

Sorry, code below. Should I be using a form action and onSubmit instead of onClick? I've been using DW for code so fairly new to custom stuff. thanks :)

 

<script type="text/javascript">

function check() {

code = document.form.input.value

if (

code == "ABC"

)

{

window.location = "yes.php"

}

else

window.location = "no.php"

}

</script>

 

<form name = "form">

<input name=input type=text autocomplete="off" onChange="javascript:this.value=this.value.toUpperCase();">

<input name="b1" type=button value="" onClick=check()>

 

</form>

Edited by drennan

Sorry, code below. Should I be using a form action and onSubmit instead of onClick? I've been using DW for code so fairly new to custom stuff. thanks :)

 

<script type="text/javascript">

function check() {

code = document.form.input.value

if (

code == "ABC"

)

{

window.location = "yes.php"

}

else

window.location = "no.php"

}

</script>

 

<form name = "form">

<input name=input type=text autocomplete="off" onChange="javascript:this.value=this.value.toUpperCase();">

<input name="b1" type=button value="" onClick=check()>

 

</form>

 

Seems to work for me

  • Author

That's weird, not working here. Tested in safari and firefox. It works if i hit the submit button but if I hit enter it returns "input=ABC" after the URL and clears the input text

Assuming your form is like this:

<form id="myform" action="">
   	<p>
       	<input name="myinput" id="myinput" autocomplete="off" type="text" />
       	<input type="submit" value="Submit" />
   	</p>
</form>

place the following js just before the </body> tag:

<script type="text/javascript">

       	window.onload = function() {
           	var myform = document.getElementById('myform');
           	var myinput = document.getElementById('myinput');
           	myinput.onchange = function() {
               	this.value = this.value.toUpperCase();
           	};
           	myform.onsubmit = check;
       	}
       	function check(){
           	var code = document.getElementById('myinput').value;
           	if (code == "ABC")
           	{
               	window.location = "yes.php";
           	}
           	else {
               	window.location = "no.php";
           	}
           	return false;
       	}
   	</script>

  • Author

Brilliant, works perfectly. Thanks for helping me out. Can you see why my code wasn't working, I see you've put the UpperCase function in the JS, was that the prob? thanks.

All I've done is move the js out of the html and use the onsubmit event instead of an onclick event .

You're using a form so the onsubmit is what you need.

This then handles the 'enter' key.

The most important line is 'return false; '.This stops the browser from completing the default action (posting the form) and is why you were seeing the parameters in the url.

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.