Do not use Javascript for this!
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 has focus by the user, autofocus
attribute is set on a form field or button, or user tab's into any of the form fields.
So instead of Javascripting this, an easier solution is to add tabindex=0
on your form fields and button inside a form element then autofocus
on the first input control. The user can then press "ENTER" to submit the form at any point as they enter data:
<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>