Grant Grant - 9 months ago 60
HTML Question

document.getelementbyId in Google Script not working

I am trying to make a google script web app that takes input from an HTML form and passes the input to a script. Right now, the function is failing because

instead of the actual form value. How can I fix this?


<!DOCTYPE html>
<base target="_top">
// Prevent forms from submitting.
function preventFormSubmit() {
var forms = document.querySelectorAll('form');
for (var i = 0; i < forms.length; i++) {
forms[i].addEventListener('submit', function(event) {

window.addEventListener('load', preventFormSubmit);

function handleFormSubmit() {
var formObject = document.getElementById('text');;

function setTable(data) {
var div = document.getElementById('output');
div.innerHTML = createTable(data);

* Adds an html table
function createTable(tableData) {
var table = document.createElement('table');
var tableBody = document.createElement('tbody');

tableData.forEach(function(rowData) {
var row = document.createElement('tr');

rowData.forEach(function(cellData) {
var cell = document.createElement('td');


<form id="myForm" onsubmit="handleFormSubmit()">
<input name="text" type="text" />
<input type="submit" value="Submit" />
<div id="output"></div>

//Initalization of global variables for use by the script's custom functions

var ss = SpreadsheetApp.openById("spreadsheetID");
var sheet = ss.getSheetByName("Sheet1");

var sportsFromSheet = sheet.getRange("D4:D12");
var namesFromSheet = sheet.getRange("B4:B12").getValues();
var timesFromSheet = sheet.getRange("A4:A12").getValues();
var NAMES = [];
var TIMES = [];

* Handles HTTP GET requests to the published web app.
* @return {HtmlOutput} The HTML page to be served.
function doGet() {
return HtmlService.createHtmlOutputFromFile('Index');

* Gets both names and Times of checked-in people from the spreadsheet from the private function getOutput.
* @return {HtmlOutput} A 2D array containing the names and times.
function getSportData(formObject) {

var OUTPUT = [

return OUTPUT;

//Puts the names of every person from an inputted sport into an array.
function getNamesInSport(input) {
var data = sportsFromSheet.getValues();

for (var i = 0; i < data.length; i++) {
if(data[i] == input){

//Puts the times of every person from an inputted sport into an array.
function getTimesInSport(input){
var data = sportsFromSheet.getValues();

for (var i = 0; i < data.length; i ++) {
if(data[i] == input){

Answer Source

Duplicate of Why does jQuery or a DOM method such as getElementById not find the element?.

You appear to have answered your own question.

document.getElementById('text') returns null because... You don't have an element with an id="text".

document.getElementById will return null

if an element with the specified ID is not in the document. (Mozilla Developer Network)

The solution, is <input type="text" name="text"id="text">