views:

266

answers:

4
<form>
   <input type="text" name="sometext">
   <input type="button" value="Send" onClick="sendsometext(this.form);">
</form>

How would that be without using the submit button (I don't want it at all), just submitting the form by pressing the Enter key.

EDITED: I don't want to submit the form at all. I just want to type some text in the input field and call the function by the Enter key. Can I do this without JS?

+4  A: 
<form>
   <input type="text" name="sometext">
   <input type="button" value="Send" onClick="this.form.submit">
</form>

Or, from the sendsometext function:

function sendsometext(form){
  form.submit();
}

To run the function when the form submits (when the user presses Enter), try the following:

<form onsubmit="sendsometext(this)">
   <input type="text" name="sometext">
</form>

If you return false from sendsometext, then the form will not submit.

Edit (Again)

Apparently you don't want to submit the form, all you want to do is let the function process the data and then do something with it. If your sendsometext function returns false, then the form should not submit:

function sendsometext(form){
  //do something with the form;
  return false;
}

and then the html code:

<form onsubmit="return sendsometext(this)">
   <input type="text" name="sometext">
</form>

If this does not work, then please specify what browser you are using, and what happens. Also post a demo page with how you have implemented it. You cannot sumbmit the form to JavaScript without the use of JavaScript (that does not make sense).

Marius
I only need a field where to type text in, and then submit by pressing the Enter key to run a JavaScript function. I do not need a button to submit the form.
Stranger
Just call `onsubmit="return sendsometext(this)'"` and have the `sendsometext()` function return false. This will stop the form submission. Alternatively, there is another answer with an example of just catching the enter key.
Khanzor
The code you sugested do not work as needed.
Stranger
+1  A: 

Actually, I think you are looking for form.onsubmit

<form onSubmit="sendsometext(this.form);">
  <input type="text" name="sometext">
  <input type="submit" value="Send">
</form>
Wogan
+1  A: 

This is how you would create a Javascript function that acts on the the enter key being pressed to submit the form, instead of pressing the submit button.

Edit: On the code below where you see document.forms[0].submit() //submit the form ... You can change this line to point towards your function. That would stop the submission process and instead call your function when you press enter.

From http://jennifermadden.com/javascript/stringEnterKeyDetector.html

<input type="text" onKeyPress="checkEnter(event)"> 


function checkEnter(e){ //e is event object passed from function invocation
    var characterCode;

    if(e && e.which){ //if which property of event object is supported (NN4)
     e = e
     characterCode = e.which //character code is contained in NN4's which property
    }
    else{
     e = event
     characterCode = e.keyCode //character code is contained in IE's keyCode property
    }

    if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)
     document.forms[0].submit() //submit the form
     return false
    }
    else{
     return true
    }
}
Maarek
+1  A: 

I wonder if the OP is missing the basic information that the browser automatically submits the form when you press Enter in any text field inside the form. That's why you can use the onsubmit handler if you want to invoke a method in response to the Enter key, per Marius' and Wogan's answers.

ykaganovich