I have a custom search page for use in Customer Portal.

VF Page:

  <form name="frmSearch">
     <input type="text" name="keyword" id="keyword" value="{!portalSearchModel.searchTerm}" onkeypress="return noenter(event);" style="width:25%; height:25px;margin:0; padding: 0px 6px 0px;" placeholder="What are you looking for?" />
     <input type="button" id="btnSearch" name="btnSearch" value="Search" onclick="searchTerm()" class="go-we3" />

 <script type='text/javascript'>  
    function searchTerm(){
        var searchTerm = document.getElementById("keyword").value; 
        var url="/apex/PortalSearch?s="+searchTerm; 
        window.location = url; 
        return false;

Is there a way to call this apex method in Controller on button click along with the JavaScript?

    public void logSearchTerm(){
        Search_Log__c sl = new Search_Log__c(); 
        sl.search_term__c = portalSearchModel.searchTerm;
        sl.user__c = UserInfo.getUserId();     
        insert sl; 
  • any reason you aren't just updated the constructor of the PortalSearch apex page? Commented Apr 8, 2013 at 21:49

2 Answers 2


One way to do it is wrapping the controller method using an actionFunction, which can then be invoked from JavaScript.

<apex:actionFunction name="logSearchTermAF" action="{!logSearchTerm}" />

function searchTerm(){

JavaScript Remoting is the other alternative.


Unless you have a specific reason for using javascript (i.e. doing front validation) you can do this a lot simpler by doing direct bindings.

Example Controller

public class SearchController {

  public String searchTerm { get; set; }

  public void doSearch() {
    // do search


Example Page

    <apex:inputText value="{!searchTerm}"/>
    <apex:commandButton value="Search" action="{!doSearch}"/>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .