function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
OldDeadBugOldDeadBug 

Handle the enter key event in an extension

I've built an controller extension to the Opportunity. I've also built some Javascript into the page where a user entering a value in one field should update the value in another.

 

The javascript is working, however if the user presses the enter key, as opposed to say the tab key, it appears that some kind of standard function in Salesforce gets called, as though the Opportunity edit page is open, and the page refreshes with the original value, defeating the javascript.It looks like the underlying Opportunity on which the page is based is updating and refreshing the VF page.

 

I can expect a user to hit the enter key as a habit when updating the field value so I need a way to stop whatever process is called when the enter button is hit in an edit page.

 

I have a javascript noenter-type routine, but I haven't seen any way to use Javascript to stop that process, probably because I'm not a javascript guru. 

 

Can I override the Opportunity Save function? Here's the code I'm using:

 

function noenterB(e){
    if(window.event){
    key = window.event.keyCode; //IE
    } else{
    key = e.which; //firefox
    }
    alert("Key is "+key);
    
    updSalT(); // this is the routine to update the other field
    }

I've tried to return null or false, but this has no effect. I don't think I can prevent or bypass whatever standard Salesforce save process is being called by pressing the enter key in Javascript, can I??

 <apex:InputText label="Project Sale $/W: " value="{!decSalBase}" id="SalB" onkeydown="noenterB(event)"/>

I was thinking of trying an actionSupport tag maybe to call some other APEX process which might bypass the save routine?

 

Any suggestions?

bob_buzzardbob_buzzard

I think the issue here is that you need to return false from your method when it detects a return key, otherwise the default browser behaviour will kick in that is to submit the form (in most cases).

function noenterB(e){
    if(window.event){
    key = window.event.keyCode; //IE
    } else{
    key = e.which; //firefox
    }
    alert("Key is "+key);
    
    updSalT(); // this is the routine to update the other field

    if (window.event && window.event.keyCode == 13 || ev.which == 13)
    {
       return false;
    }
    else
    {
       return true;
    }
}