Submit Form with Enter key

Hi,

How can I submit a form with the Enter key :question:

Frank

Unfortunately it not possible to submit form with Enter key

What?! Really not possible?

dhtmlx and jQuery does not conflict,
So we can also include jquery library and use jquery event handler.

like this.

[code]

$(‘input’).keypress(function(event){
// alert(event.which);
});
[/code]

I was wrong.
I tried to submit form by jQuery function “trigger”,
but it doesn’t work.
Couldn’t submit form by enter key.

You can extend dhtlmxForm (v.3.0 Standard edition build 110713) to provide such functionality.

First, you have to add this code at the beginning of your custom js script.

[code]//Extend dhtmlxForm

/input/
dhtmlXForm.prototype.items.input .doAttachEvents= function(item) {

item.childNodes[1].childNodes[0].onblur = function() {
if (item._value != this.value) {
    if (item.checkEvent("onBeforeChange")) if (item.callEvent("onBeforeChange",[this._idd, item._value, this.value]) !== true) {
	// restore
	this.value = item._value;
	return;
    }
    // accepted
    item._value = this.value;
    item.callEvent("onChange",[this._idd, this.value]);
}
}

item.childNodes[1].childNodes[0].onkeypress = function(e) {
if (e.charCode== 13) { // ENTER pressed !
    item.callEvent("onEnterPressed",[this._idd,this.value]);
}
}

}

/* password */
dhtmlXForm.prototype.items.password.doAttachEvents= function(item) {

item.childNodes[1].childNodes[0].onkeypress = function(e) {
if (e.charCode== 13) { // ENTER pressed !
    item.callEvent("onEnterPressed",[this._idd,this.value]);
}
}    

};[/code]

From then, you should have a new global event called “onEnterPressed” that will be called for text and password input.

For instance if you form object is named myform :

myForm.attachEvent("onEnterPressed", function (id, value){ // Custom code });

Update code that fix a issue with former event on pasword.

[code]//Extend Form

/input/
dhtmlXForm.prototype.items.input .doAttachEvents= function(item) {

item.childNodes[1].childNodes[0].onblur = function() {
if (item._value != this.value) {
    if (item.checkEvent("onBeforeChange")) if (item.callEvent("onBeforeChange",[this._idd, item._value, this.value]) !== true) {
	// restore
	this.value = item._value;
	return;
    }
    // accepted
    item._value = this.value;
    item.callEvent("onChange",[this._idd, this.value]);
}
}

item.childNodes[1].childNodes[0].onkeypress = function(e) {
if (e.charCode== 13) { // ENTER pressed !
    item.callEvent("onEnterPressed",[this._idd,this.value]);
}
}

}

/* password */
dhtmlXForm.prototype.items.password = {

render: function(item, data) {
    
    item._type = "pw";
    item._enabled = true;
    
    this.doAddLabel(item, data);
    this.doAddInput(item, data, "INPUT", "PASSWORD", true, true, "dhxlist_txt_textarea");
    this.doAttachEvents(item);
    
    item._value = (data.value||"");
    item.childNodes[1].childNodes[0].value = item._value;
    
    return this;
    
},
doAttachEvents: function(item) {

item.childNodes[1].childNodes[0].onkeypress = function(e) {
    if (e.charCode== 13) { // ENTER pressed !
	item.callEvent("onEnterPressed",[this._idd,this.value]);
    }
}
}

};

(function(){
for (var a in {doAddLabel:1,doAddInput:1,doAttachEvents:1,destruct:1,doUnloadNestedLists:1,setText:1,getText:1,setValue:1,getValue:1,enable:1,disable:1,setWidth:1,setReadonly:1,isReadonly:1,setFocus:1,getInput:1})
dhtmlXForm.prototype.items.password[a] = dhtmlXForm.prototype.items.input[a];
})();[/code]

Code updated to handle correctly validation events.

[code]//Extend dhtmlxForm

/input/
dhtmlXForm.prototype.items.input .doAttachEvents= function(item) {

item.childNodes[1].childNodes[0].onblur = function() {
if (item._value != this.value) {
    if (item.checkEvent("onBeforeChange")) if (item.callEvent("onBeforeChange",[this._idd, item._value, this.value]) !== true) {
	// restore
	this.value = item._value;
	return;
    }
    // accepted
    item._value = this.value;
    item.callEvent("onChange",[this._idd, this.value]);
}
}

item.childNodes[1].childNodes[0].onkeypress = function(e) {
if (e.charCode== 13) { // ENTER pressed !
    
    if (item._value != this.value) { // Validation check
	if (item.checkEvent("onBeforeChange")) if (item.callEvent("onBeforeChange",[this._idd, item._value, this.value]) !== true) {
	    // restore
	    this.value = item._value;
	    return;
	}
	// accepted
	item._value = this.value;
	item.callEvent("onChange",[this._idd, this.value]);
    }
    
    item.callEvent("onEnterPressed",[this._idd,this.value]);
}
}

}

/* password */
dhtmlXForm.prototype.items.password = {

render: function(item, data) {
    
    item._type = "pw";
    item._enabled = true;
    
    this.doAddLabel(item, data);
    this.doAddInput(item, data, "INPUT", "PASSWORD", true, true, "dhxlist_txt_textarea");
    this.doAttachEvents(item);
    
    item._value = (data.value||"");
    item.childNodes[1].childNodes[0].value = item._value;
    
    return this;
    
}

};

(function(){
for (var a in {doAddLabel:1,doAddInput:1,doAttachEvents:1,destruct:1,doUnloadNestedLists:1,setText:1,getText:1,setValue:1,getValue:1,enable:1,disable:1,setWidth:1,setReadonly:1,isReadonly:1,setFocus:1,getInput:1})
dhtmlXForm.prototype.items.password[a] = dhtmlXForm.prototype.items.input[a];
})();[/code]

I had a little problem with this function. The function looks at the charCode to see if an enter key was pressed. It is expecting a value of 13 which is the ASCII value for carriage return. However, FireFox 7.0.1 on Mac OS X 10.7.2 was returning 0 (a null char). I changed charCode to keyCode and let the value as 13. This seems to work find on both Mac and Windows with Firefox.

asciitable.com/
cambiaresearch.com/articles/ … -key-codes

New Code

    item.childNodes[1].childNodes[0].onkeypress = function(e) {
   if (e.keyCode== 13) { // ENTER key pressed !

Old Code

    item.childNodes[1].childNodes[0].onkeypress = function(e) {
   if (e.charCode== 13) { // ENTER pressed !

Cheers,
Eric

Hi to all

Not bad, new version of form already have “onKeyUp” event for inputs (if anyone interested in updated form please contact us via support(at)dhtmlx.com).
Also you can call myForm.getInput(name) which will return you DOM input element and also add kbd event handler to it.

Is this included in the latest release? Can you post an example.

Thank you.

This will included into future relase (both onKeyUp and onEnter events)

Is this implemented yet?

myForm.attachEvent("onButtonClick", callback); // Works myForm.attachEvent("onEnter", callback); // Does not work myForm.attachEvent("onKeyUp", callback); // Does not work

Here are actual working dhxform event handlers:
docs.dhtmlx.com/doku.php?id=dhtm … vents_form

So they both don’t work in Chromium 29 and Chrome 30.

Please, provide us a direct link or demo to test this issue.
docs.dhtmlx.com/doku.php?id=othe … leted_demo
Locally everything is ok.

You are correct!
I was just building a testcase, and it turns out it works. I must have borked something in my app.
Sorry for wasting your time. :blush:

The problem is caused when using a development library like RequireJS and you reference static functions like self.callback.

Something like that, because in theory it should work. I borked something here.

Disregard! :smiley:

Kids, don’t forget to bind objects: self.callback.bind(self) :slight_smile: