+ Start a Discussion
aebenaeben 

How to call a customer controller action method from Java Script

Visualforce newbie.
 
I wonder if it is possible to call a customer controller action from java script.
 
here is what i want to do:
 
I have a input field and a button. I have binded the button to an action from the controller. No issues there. I have added a onkeypress on the input field. So that when the enter key is pressed, I want to call the action method bound to the button.
 
Thanks in advance.
 
TehNrdTehNrd
Yes. Here is an example.

Code:
<script type="text/javascript">
  function load(){
    recalc();
  }
  window.onload=load;
</script>

<apex:actionFunction name="recalc" action="{!calcPricing}" rerender="tables,info" status="status"/>

 

aebenaeben

Thanks for response.

But, it did not work the way I expected it to.

<script type="text/javascript">

function submitEnter(e)

{

if (null == e) e = window.event ;

if (e.keyCode == 13)

{

searchQuery();

}

}

</script>

<apex:actionFunction name="searchQuery" action="{!query}" rerender="results,noresults" status="myStatus"/>

<apex:inputText id="queryTerm" value="{!queryTerm}" tabIndex="1" maxlength="255" onkeypress="submitEnter(event)"/>

<apex:commandButton value="Search" rerender="results,noresults" status="myStatus" action="{!query}"/>

When I click on the command button it working fine. Meaning only the outputPanel is getting refreshed.

When I press enter, the whole page is refreshing.

TehNrdTehNrd
I think hitting enter is a finicky thing Visualforce does where you can't really controller what button is selected when hitting enter. Someone else may be able to confirm.
JohanLiljegrenJohanLiljegren
I've been trying to get this to work myself, but can't seem to get quite there...
Did any of you ever arrive at a working piece of code you would care to share?
creemejcreemej
Hi,

I think that the reason is, that we do not have access to the event object.
When I verify 'window.event' it is 'undefined'.

Is there another way to handle such things, like determine which key was pressed in an onkeypress and the other events.

Can someone of the SFDC-team explain why this is not evailable?

thanks,

Jan

JohanLiljegrenJohanLiljegren

creemej, what browser are you using that tells you window.event is undefined?

 

I've successfully used the following function with IE 6, Firefox 3, Chrome and Safari 3.2.

 

function submitEnter(myField, evt) { if (null == evt) { evt = window.event; } evt = (evt) ? evt : event; //IE and others behave differently var target = (evt.target) ? evt.target : evt.srcElement; var form = target.form; var charCode = (evt.charCode) ? evt.charCode : ((evt.which) ? evt.which : evt.keyCode); if (charCode == 13 || charCode == 3) { //Do what you want to do here... return false; } return true; }

 

...and call that function with onkeypress in your input field:

 

<apex:inputText onkeypress="return submitEnter(this, event)" />
Message Edited by JohanLiljegren on 01-22-2009 12:36 PM