views:

827

answers:

4

I need to have an input box in a div without a form and when the user enters something and hits return it runs a javascript function

there will be no submit button.

how do I do this?

Thanks in advance of your help.

Jonathan

A: 

To get an input box without a form, I would suggest just not using a form.

Reinis I.
Yeah, you will just accrue validation errors if nothing else.
No, input without form is perfectly valid [X]HTML.
bobince
chibineku: in addition to what bobince said: the HTML 4.01 standard specifies that "The elements used to create controls [e.g. input elements] generally appear inside a FORM element, but may also appear outside of a FORM element declaration when they are used to build user interfaces. " (http://www.w3.org/TR/html401/interact/forms.html#form-controls)
simon
A: 

To run a function when the user presses enter when focused on the input field, the easiest way would be to have the form tag and run the function using the form's onsubmit event (because pressing return when focused on an input field submits the form).

If you nevertheless don't want to include the form tag, you can use the onkeypress event to catch the return key pressing and call the function from there.

simon
A: 

Don't use the form tag. You can easily use JQuery to tie the code to the input control's key press events.

If you are unfamiliar with JQuery, just tie the code directly to the input field key press event.

Krish
A: 

You will have to attach a onkeypress event and check if enter was pressed (and rune the code if it was). Tell us if you are using plain JavaScript or some library if you need examples.

Jan Hančič