I have a form with a few textboxes in it and a few buttons. I have a couple custom form elements that I am working on. One, in particular, is a textbox that will search a database onEnterClicked. This works just fine, but I also have a button that will run code onClick. Both of these appear to be linked to submitting the form.
<form onsubmit="return false;"> <input type="text" id="autofill"> ... <button id="upload"> When I run this jQuery code:
$("input#autofill").keyUp(function(e){ //Do stuff }); $("button#upload").click(function(){ alert("test"); }); Pressing enter in the autofill textbox will show the test alert, but will not do any of the //do stuff code.
How can I prevent this from happening?
$(function(){ $("#autofill").keyup(function(e){ if(e.keyCode == 13) alert("Enter pressed"); }); $("#upload").click(function(){ alert("Button clicked"); }); }); <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <form onsubmit="return false;"> <input type="text" id="autofill"/> <button id="upload">Click me to show an alert</button> </form>
//do stuffto run when I press enter in the text box; right now it is showing thealert("test"), when it should not be, as I show in the snippet. Try pressing enter in the text box. It will show "Button Clicked" instead of "Enter pressed"