October 28, 201115 yr 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
October 28, 201115 yr Can you show us some code? If you're using a form, the onsubmit event is what you want to look for.
October 28, 201115 yr 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 October 28, 201115 yr by drennan
October 28, 201115 yr 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
October 28, 201115 yr 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
October 29, 201115 yr 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>
October 30, 201115 yr 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.
October 30, 201115 yr 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