Antonio Mercado
Antonio Mercado

Reputation: 55

Google App Script and Javascript: Accessing a form elements inside div tags

I'd like to access an input element inside a form and a div. I'm new to Google app script and I believe I need to pass an argument that contains the DOM address from a JS function to an app script function.

Here's the code:

Javascript:

<script>
            function update()
                {
                    google.script.run.selectCell(document.getElementById(location));

                    alert("Success");//test message
                }
</script>

HTML code:

<div class="container">
  <form>

    <div class="row">

      <div class="col-25">
        <label for="location">Location</label>
      </div>

      <div class="col-75">
        <input type="text" id="location" name="location_txt" placeholder="The location..">
      </div>

    </div>

    <div class="row">
      <input type="button" onclick="update()" value="Update">
    </div>

Google App Script:

    function selectCell(domAddress)
        {
          var val0 = domAddress.value;
          if (cell)
          {
            sheet.appendRow([val0]);
          }
        }

Upvotes: 0

Views: 1219

Answers (1)

TheMaster
TheMaster

Reputation: 50799

Issues:

  • DOM Elements, except form element are not legal parameters to server side functions

Solution:

  • Pass the form itself to server-side function, which will be converted to a form object.

Modified Code:

ClientSide:

<div class="container">
    <form id ="myForm">

        <div class="row">

          <div class="col-25">
            <label for="location">Location</label>
          </div>

          <div class="col-75">
            <input type="text" id="location" name="location_txt" placeholder="The location..">
          </div>

        </div>

        <div class="row">
          <input type="button" onclick="update()" value="Update">
        </div>
    </form>

<script>
function update()
{
google.script.run.selectCell(document.getElementById('myForm'));

alert("Success");//test message
}
</script>

Server-side:

function selectCell(formObject)
    {
      var val0 = formObject['location_txt']
      if (val0)
      {
        sheet.appendRow([val0]);
      }
    }

To Read:

Upvotes: 1

Related Questions