I searched here before posting this, but could not find any solution for this.
I have a simple text box with a submit button. This is inside a Form tag. I want that the form gets submitted when the user presses enter in the textbox. The submit right now calls another function which gets callback values from the dao. I have not been able to implement this enter functionality and its bugging me now.
I even tried onKeyPress instead of onChange and that did not work either.
The code for the form is:
<div id="leftTop">
<form dojoType="dijit.form.Form" id="myForm"
encType="multipart/form-data" action="" method="">
<h3>
Enter Order Number
</h3>
<input dojoType="dijit.form.TextBox" value="Enter Order Number"
id="orderNumberInput" trim="true" propercase="true" onChange="checkEnter(event)"/>
<button dojoType="dijit.form.Button" id="searchOrderButton"
onclick="orderSearchHandler(document.getElementById('orderNumberInput'))">Lookup</button>
</form>
</div>
<form dojoType="dijit.form.Form" id="myForm"
encType="multipart/form-data" action="" method="">
<h3>
Enter Order Number
</h3>
<input dojoType="dijit.form.TextBox" value="Enter Order Number"
id="orderNumberInput" trim="true" propercase="true" onChange="checkEnter(event)"/>
<button dojoType="dijit.form.Button" id="searchOrderButton"
onclick="orderSearchHandler(document.getElementById('orderNumberInput'))">Lookup</button>
</form>
</div>
The checkEnter javascript
//function for forcing form submission
function checkEnter(e){
var characterCode
if(e && e.which){
e = e
characterCode = e.which
}
else{
e = event
characterCode = e.keyCode
}
if(characterCode == 13){
document.forms[0].submit()
return false
}
return true
}
function checkEnter(e){
var characterCode
if(e && e.which){
e = e
characterCode = e.which
}
else{
e = event
characterCode = e.keyCode
}
if(characterCode == 13){
document.forms[0].submit()
return false
}
return true
}
The onChange attribute was something I was trying to implement to capture the key press 13 with a javascript, but that din't work either.
What am I doing wrong? or not doing to get the form to submit on the enter key press?

Give this a try...
I'm not sure if I understand your intentions completely, but I figured I'd give it a shot. Try the HTML / JavaScript below, and see if that is the behavior you're looking for.
I'm not sure which of the Widgets or Elements are picking up the Enter key in this scenario, but it works for me using what I did below...
When using a dijit.form.Form, it will call its own onSubmit function BEFORE actually submitting the form, passing the Event Object that was handed down by the real Form submit in the browser. This allows you, the developer, to intercept this request before-hand and do whatever you need.
In this case what I did, was tell the dijit.form.Form to call the handleFormSubmit function when it calls its own onSubmit function. The handFormSubmit function then calls dojo.stopEvent(evt); to stop the normal Form Submti from happening. It then allows you to do whatever calls you need to do, without the form actually submitting.
If you did, however, want to submit the form in the end (e.g. you only want to intercept the submit for validation and the like), then you can always follow-up with a .submit() on the dijit.form.Form you're using.
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=7" />
<title>Testing dijit.form.Form</title>
<style type="text/css">
<!-- --------------------------- -->
<!-- INCLUDE DOJO CSS FILES HERE -->
<!-- --------------------------- -->
</style>
<!-- ----------------------------------- -->
<!-- INCLUDE DOJO RELEASE HERE (dojo.js) -->
<!-- ----------------------------------- -->
<script type="text/javascript">
dojo.require("dojo.parser");
dojo.require("dijit.form.Form");
dojo.require("dijit.form.TextBox");
dojo.require("dijit.form.Button");
function handleFormSubmit(/*Event*/ evt) {
/* Stop the event from propogating and 'REALLY' submitting the form. */
dojo.stopEvent(evt);
/* Disable the 'Lookup' button while the form is being handled. */
dijit.byId("searchOrderButton").setAttribute('disabled', true);
/* Call the orderSearchHandler function to do whatever it is the
* orderSearchHandler does! :)
*/
orderSearchHandler(document.getElementById('orderNumberInput'));
/* Re-enable the 'Lookup' button after the form is handled. (Should be
* moved to a callback or something of the sort)
*/
dijit.byId("searchOrderButton").setAttribute('disabled', false);
};
</script>
</head>
<body class="tundra">
<div id="leftTop">
<form dojoType="dijit.form.Form" id="myForm" onSubmit="handleFormSubmit" encType="multipart/form-data" action="" method="">
<h3>Enter Order Number</h3>
<input dojoType="dijit.form.TextBox" value="Enter Order Number" id="orderNumberInput" trim="true" propercase="true" />
<button dojoType="dijit.form.Button" type="submit" id="searchOrderButton">Lookup</button>
</form>
</div>
</body>
</html>
use type="submit"
Set the 'type' parameter on your button to "submit".
My understanding is that the first button or input tag with type="submit" will be the default button for that form. That means that pressing the Enter key on any field on that form will function the same as pressing that default button.
What I think is the point
Hi everyone,
and especially those who saw this as the first search result on google, like I did.
I had this problem, too. And I wasn't able to find a solution either - or better, I didn't see the point. That is, in my opinion:
Most of the time everybody is happy with the browser's standard implementation, that automatically submits a form, if someone presses the enter (or return) button inside of an input field that is inside the form (as bsenninger says).
But sometimes, you don't have a surrounding form (like I had, because of a surround dijit dialog, that "executes" when hitting the submit button - i didn't understand that completely) or you want to do something else when pressing the enter button, instead of submitting the form (like I did too).
So, you have multiple choices to solve those problems:
1. Stopping the standard submit (as jgarfield says) an implementing something else. That would work, if you have a surrounding form.
2. Or implementing an onkeypress-Method, that catches the ENTER keyCode and does what you want to.
In my case I had to use the second one. And also tried to manually set the onchange or onkeypress parameters. But that doesn't work, because when the dijit button is rendered these parameters are going kicked out the markup. Instead the textbox markup template is used (see /dijit/form/TextBox.js and /dijit/form/_FormWidget.js with the _onKeyPress function that takes the place).
So now, there are another two options to solve this:
1. Write your own TextBox widget, that inherits from the dijit TextBox (perhaps you just want to change the template string).
2. Or (like I did) add a connect to the input field's onkeypress event, that then is executed aditionally to dijit's _onKeyPress.
Here is a bit of code for that.
function init() { dojo.connect(dojo.byId("yourInputField"), 'onkeypress', function(e) { if (e.keyCode == dojo.keys.ENTER) { //do whatever var d = dijit.byId("myDialog"); d.execute( d.getValues() ); } }); } dojo.addOnLoad(init);Hope it helps :-)
Daniel
Yes, it helps :-)
I planned to create a new widget from textbox to get the same behaviour, when I saw your suggestion, it works well. Thanks