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
Edwin KEdwin K 

Handle ENTER event key press in visual force page

Hi all,

 

I want to ask how to handle ENTER event key press in visual force page? Is it possible using javascript or any suggestions? My case is I have 2 buttons in visualforce page and if I click ENTER regardless where I hit ENTER, I want my button no.2 to be run.. Is that possible?

Any suggestions would be great..

 

Thanks

Best Answer chosen by Admin (Salesforce Developers) 
Navatar_DbSupNavatar_DbSup

Hi,

     You can implement this through javacript code.Below is the code snippet to attain your requiremnt.

 

<apex:inputText id="username" onkeypress="return onKeyup_TxtFieldcom(event);" value="{!username}" />
<script>
function onKeyup_TxtFieldcom(e)
{
var keynum;
if(window.event) // IE
{
keynum = e.keyCode;
}

if(keynum == 13) 
{ 
redirct();
return false; 
} 
}
</script>

 Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

All Answers

Navatar_DbSupNavatar_DbSup

Hi,

     You can implement this through javacript code.Below is the code snippet to attain your requiremnt.

 

<apex:inputText id="username" onkeypress="return onKeyup_TxtFieldcom(event);" value="{!username}" />
<script>
function onKeyup_TxtFieldcom(e)
{
var keynum;
if(window.event) // IE
{
keynum = e.keyCode;
}

if(keynum == 13) 
{ 
redirct();
return false; 
} 
}
</script>

 Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

This was selected as the best answer
Edwin KEdwin K

Hi,

 

thanks for the solution.. It works pretty well.. I added a few codes so it can run in Firefox too..

Thanx a lot..

 

 

Bhaskar AroraBhaskar Arora
For calling the above method on Enter Click.
Use : window.onkeypress=onKeyup_TxtFieldcom;
under your script.