Do not use Javascript for this solution!!!
Modern HTML pages automatically allow a form's submit button to submit the page with the ENTER/RETURN key when any form field control in the web page that is associated with a submit type button or input has focus by the user, autofocus attribute is set on any form field, button, or input, or the user tab's into any of the form fields. Pressing ENTER or RETURN on the keyboard then automatically triggers the first available submit button or input control for that form.
So, instead of JavaScripting this, an easier solution is to just add tabindex=0 on any of your form fields or submit buttons inside a form element then autofocus on the first input control or submit button. Tabindex=0 assigns that input to the page's list of indexed tab order page items, and autofocus shifts focus to any of your form fields, triggering any submit button to respond to the ENTER/RETURN key command. The user can now press "ENTER" on their keyboard to submit the form at any point they like. This also has the advantage that the first form field is focused on and ready for data by the user. An example below:
<form id="buttonform2" name="buttonform2" action="#" method="get" role="form">
<label for="username1">Username</label>
<input type="text" id="username1" name="username" value="" size="20" maxlength="20" title="Username" tabindex="0" autofocus="autofocus" />
<label for="password1">Password</label>
<input type="password" id="password1" name="password" size="20" maxlength="20" value="" title="Password" tabindex="0" role="textbox" aria-label="Password" />
<button id="button2" name="button2" type="submit" value="submit" form="buttonform2" title="Submit" tabindex="0" role="button" aria-label="Submit">Submit</button>
</form>
Stop scripting everything! The browsers have had this native ability for almost 20 years!!