This topic is locked
[SOLVED]

 how to use Enter/ESC key to Save/Cancel

7/18/2011 1:07:24 AM
PHPRunner General questions
P
paulirvine author

the section 508 optoin lets you use ALT-S and ALT-C to save and cancel respectively, but how would one best approach setting the Enter key to Save, and ESCape key to cancel? (I'd rather not hack the code so solution needs to be in events or... )

many thanks for any ideas.

Paul

C
cgphp 7/18/2011

In the javascript onLoad event:

$(document).keyup(function(e) {

if (e.keyCode == 13) { // trigger the save function }

if (e.keyCode == 27) { // trigger the cancel function }

});
P
paulirvine author 8/20/2011



In the javascript onLoad event:

$(document).keyup(function(e) {

if (e.keyCode == 13) { // trigger the save function }

if (e.keyCode == 27) { // trigger the cancel function }

});



Thanks Cristian, I didnt see this response. i'm new ot the forum too and didnt realize i had to enable email notification of replies!

you'll have to pardon a newb phprunner ... the save function is presumably firing a click on the "save" button? And the cancel function would typically be the "return to list" button on most Add / edit pages. How exactly does one fire those buttons in the onload event?
Do you know if there is a way to modify the default Add or Edit page template to automatically do this for every page? For me, it will be the exception that I would want to stay in 'add mode'.
Many thanks

Paul

C
cgphp 8/20/2011
$(document).keyup(function(e) {

if (e.keyCode == 13)

{

$("input[id^='saveButton']").trigger('click');

}

if (e.keyCode == 27)

{

$("input[id^='cancelButton']").trigger('click');

}

});


If you want to do this automatically for each add/edit page you have to include the script as external file (http://xlinesoft.com/phprunner/docs/how_to_add_external_files.htm).

D
danaci 8/21/2011

hi,

is it possible when press enter key to next field? or pres up/down key next/before field?

regards.

C
cgphp 8/21/2011
$(document).keyup(function(e) {

switch(e.keyCode)

{

case 13:

$("input[id^='saveButton']").trigger('click');

break;
case 27:

$("input[id^='cancelButton']").trigger('click');

break;
case 38:

$("input[id^='nextButton']").trigger('click');

break;
case 40:

$("input[id^='prevButton']").trigger('click');

break;

}

});
D
danaci 8/22/2011

Many thanks!

P
paulirvine author 8/22/2011

Thanks Cristian - greatly appreciated.

Paul


$(document).keyup(function(e) {

if (e.keyCode == 13)

{

$("input[id^='saveButton']").trigger('click');

}

if (e.keyCode == 27)

{

$("input[id^='cancelButton']").trigger('click');

}

});


If you want to do this automatically for each add/edit page you have to include the script as external file (http://xlinesoft.com/phprunner/docs/how_to_add_external_files.htm).

P
paulirvine author 8/22/2011

odd. I must still be doing something wrong. That looked like an ideal solution, but it doesnt work for me at all.
i input your example code in onpageload event for the add screen directly to test it. it didnt register either enter or escape key (firefox 6.0).

using firebug, I see that the standard Save button has an id = saveButton1. I changed to trigger click on that id too, but it still doesnt work. I'm just using ordinary html page for my add page (not the popup style and no ajax search).
tried in chrome too. no luck. I can see in firebug that the javasscript code is being added to the page head okay.

C
cgphp 8/22/2011

In firefox 6, it works for me. The only difference in the code is for the back button:



$(document).keyup(function(e) {

if (e.keyCode == 13)

{

$("input[id^='saveButton']").trigger('click');

}

else if (e.keyCode == 27)

{

$("input[id^='backButton']").trigger('click');

}

});


To avoid double check, use else if.

P
paulirvine author 8/23/2011

How bizarre... I noticed that some other code I had in "onpageload" also seemed to be suddenly not working (to uppercase on a control). But that same caode used to work, and was still working on another similar page. So I did a 'reset' on the page, then put back in the uppercase code, and the keyup code, and suddenly everything is working fine.
It would appear that the code generation gets a little confused from time to time?
thanks for your opinions on this,
paul



odd. I must still be doing something wrong. That looked like an ideal solution, but it doesnt work for me at all.
i input your example code in onpageload event for the add screen directly to test it. it didnt register either enter or escape key (firefox 6.0).

using firebug, I see that the standard Save button has an id = saveButton1. I changed to trigger click on that id too, but it still doesnt work. I'm just using ordinary html page for my add page (not the popup style and no ajax search).
tried in chrome too. no luck. I can see in firebug that the javasscript code is being added to the page head okay.

C
cgphp 8/23/2011

uppercase ?

P
paulirvine author 8/23/2011



uppercase ?


yes, I was applying uppercase to a control using addstyle. It was working fine, but I noticed while editing for the enter/esc key code that the uppercase code has stopped working on that page. most strange. I manually cleared the onpageload code and re-typed it, but it still wouldnt work. then finally used hte 'reset event' to clear that event, and re-input the code, and now both functions (uppercase, and enter key event bind) are working correctly! Most odd.
I thank you for your code and assistance Cristian. greatly appreciated, and I've learned a few things along the way.
Paul