-4

I'm coming in from this post: Input max equal to javascript variable but it did not quite solve my issue. I'm currently working with Google's HTML Service and I need help with the case below...

If I have a Code.gs function like this: var myVar = function() { var x=2; return (x+x); }

... and then, inside my HTML file, I have an input tag written like this: <input id... max=myVar>

Would setting the max attribute like this: .setAttribute("max", myVar) successfully change that value based on the value returned from my custom function? Initial attempts are failing.

  • 3
    You should try it and tell us if it works or not :) – ControlAltDel Jun 20 '19 at 13:50
  • 1
    Please post your try and result. Or you can just post `code snippet` we can see how it is going. Thanks – George Wang Jun 20 '19 at 14:01
  • Attribute `max` should be value not function. Just replace with `myVar()`. And in html , you can set `max` with function. And `setAttribute` will work as expected. Thanks – Alona Jun 20 '19 at 14:12

2 Answers2

1

Yes you can do it, but you there needs to be a few improvemens to your code:

  • function() must be lower, javascript is case sensitive
  • you have to add () after myVar to call it as a function
  • max will be checked when the form is submitted

var myVar = function() { var x=2; return (x+x); }

document.querySelector("#myInput").setAttribute("max", myVar());
<form>
  <input type="number" id="myInput" max>
  <button>send</button>
</form>
-1

If your myVar variable is supposed to get a result from myFunction() (located inside Code.gs), and you want use the result from the said function inside your HTML file, you will find that simply doing this will not work:

var myVar = function() { google.script.run.withSuccessHandler(onSuccess).myFunction()}

The above function will simply return undefined

In order to get the resultant value from myFunction(), and use it to change an input tag attribute inside your HTML file, you will have to write something like this:

function onSuccess(myVar) {
    document.getElementById(*yourID*).setAttribute(*yourTargetAttribute*, myVar);
}; google.script.run.withSuccessHandler(onSuccess).myFunction();

By doing this, we use the onSuccess callback function to store the result from myFunction(), after which we can easily use that result to change any attribute we like. I hope this helps someone because it certainly solved a major issue for me.

NOTE: Whereas the answer by Exceptional NullPointer works in most cases, for my particular case, the answer I have provided here is the one that worked.