Best way to set a default button (or trigger its event in javascript) for an input field, not part of a form
Posted
by
Sheldon Pinkman
on Stack Overflow
See other posts from Stack Overflow
or by Sheldon Pinkman
Published on 2013-10-25T09:48:32Z
Indexed on
2013/10/25
9:54 UTC
Read the original article
Hit count: 182
I've got a stand-alone input field, not part of any form.
I also got a button, that has some onclick event.
When I type something in the input field, and press the Enter key, I want it do effectively press the button, or trigger its onclick event. So that the button is "the input field's default button" so to speak.
<input id='myText' type='text' />
<button id='myButton' onclick='DoSomething()'>Do it!</button>
I guess I can mess around with the input field's onkeypress or onkeydown events and check for the Enter key, etc.
But is there a more 'clean' way, I mean something that associated the button with that input field, so that the button is the 'default action' or something for that field?
Note that I'm not inside a form, I am not sending, posting, or submitting something. The DoSomething() function just changes some of the HTML content locally, depending on the text input.
© Stack Overflow or respective owner