1

Can any one tell me how to call JavaScript function from jquery.

Jquery:

    $(document).ready(function()
    {
                  $('.email2Class').bind("cut copy paste",function(e) 
                  {
                      e.preventDefault();
                  });
callJavascript();//call javascriot from here
              });

functioncallJavascript()
{
...
...
}

Basically I am validating a visualforce form through Jquery and after validations I am trying to call a controller method, as there is no way to call a controller method from Jquery as far as I know. So, I want to invoke JavaScript method which in turn will invoke the controller method.

Note: Above code is just a sample one.

Thanks!

2 Answers 2

3

jQuery is JavaScript. Just call the function normally. You can call an actionFunction, RemoteAction, and so on.


As a trivial example, here's a simple incremental controller. Every time you call increment, the value increases by 1:

public class counterController {
    public Integer counter { get; set; }
    public counterController() {
        counter = 0;
    }
    public void increment() {
        counter++;
    }
}

In the page, I'll use jQuery to bind to the keypress event, which in turn will call the controller's "increment" function:

<apex:page controller="counterController">
    <script 
    src="https://code.jquery.com/jquery-3.1.0.slim.min.js"  
    integrity="sha256-cRpWjoSOw5KcyIOaZNo4i6fZ9tKPhYYb6i5T9RSVJG8=" 
    crossorigin="anonymous">
        </script>
    <script>
    $(function() {
        $(window).on("keypress", function() {
            alert("I'm about to increment the counter.");
            incrementCounter();
        });
    });
    </script>
    <apex:form id="form">
        Press a key to increment the counter.
        <apex:actionFunction name="incrementCounter" action="{!increment}" reRender="form" />
        The counter is now {!counter}.
    </apex:form>
</apex:page>
5
  • Cab you please elobrate? Aug 7, 2016 at 20:31
  • I tried calling it normally it is not working and my method should be called on after validation successful from jquery Aug 7, 2016 at 20:34
  • @PraveenBiyani I've included a trivial example that demonstrates how it could work. You might want to include a specific example of what's not working so we have something to troubleshoot.
    – sfdcfox
    Aug 7, 2016 at 20:48
  • I have tried the same thing but my controller method is not calling it. Aug 8, 2016 at 17:23
  • Hi @sfdcfox When ever actionFunction is getting called the browser is getting refreshed and my controller method is also not called. Any help on this? Aug 8, 2016 at 19:28
1

You can use this type of code with JavaScript Remoting for Apex Controllers

Visualforce

<script type="text/javascript">
 var j$ = jQuery.noConflict(); 
enter j$(document).ready(function(){
j$("#Submit").click(function() 
    {       
      MyController.updateMethod("ABC", function(result, event){
            console.log(result);
        });
    }); 

});
</script>

Controller

public class MyController{
public list <MyObject__c> pub {get;set;}
public MyController(ApexPages.StandardController controller) {
}


@RemoteAction
public static void updateMethod(MyObject__c myObj)
{
    //do some update

    update myObj;
}
} 

You must log in to answer this question.

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