You need to sign in to do that
Don't have an account?
aeben
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.
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.
Did any of you ever arrive at a working piece of code you would care to share?
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
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: