I have a HTML form which contains a input text and a button.
HTML
<form id="form1">
<input type="text" />
<button onclick='YouClick();'>Click</button>
</form>
JS function
function YouClick() {
alert("You just clicked");
}
When user click the button , the javascript function YouClick executes. But if i pressed enter key on input text fields than also YouClick function executes. How can i stop this behaviour ? So that only by clicking the button the YouClick function executes.
You are submitting the form, then the button which by default is
type='submit'is triggered, you can set the type attribute of the button asbutton: