Roshan Jha
Roshan Jha

Reputation: 100

call google app script user defined method on JavaScript event

I have lot of anchor tags in html with attribute data-action="someAction".
i want to call these action from 1 JavaScript on-click event like below :

But this test throwing error

Uncaught TypeError: google.script.run.withSuccessHandler(...).withFailureHandler(...).withUserObject(...).action is not a function

Update:

As i have checked console.log(action) is a string type, is there any way to make it as method of Google App Script so that my below script can work ?

My script:

$('.icon-action').on("click", function() {
    let action = $(this).attr("data-action");
      google.script.run
          .withSuccessHandler(
              function(returnSuccess, element) {
                  // return success
              })
          .withFailureHandler(
              function(msg, element) {
                   // return error
              })
          .withUserObject(this)
          .action();
  });

Upvotes: 0

Views: 109

Answers (1)

Roshan Jha
Roshan Jha

Reputation: 100

With the help and suggestion of Oleg Valter i solved this issue. for future users i am posting my working solution here. Please refer to this comment - call google app script user defined method on JavaScript event

$('.icon-action').on("click", function() {
    let action = $(this).attr("data-action");
      google.script.run
          .withSuccessHandler(
              function(returnSuccess, element) {
                  // return success
              })
          .withFailureHandler(
              function(msg, element) {
                   // return error
              })
          .withUserObject(this)[action]();
  });

Upvotes: 1

Related Questions