'document.getElementById "not defined" in Google Apps Script [duplicate]
I am currently working on a Google forms addon. The addon has a sidebar with an input field and a button. The ID of the input field is "emailinput". The user enters an email address into the input text field, and hits send. This sends him an email. However, when I try to get the value of the input field using var email = document.getElementById("emailinput").value
i get an error saying "document" is undefined. Here is my code:
Code.gs
function element() {
var textinput = document.getElementById("textfield").value
MailApp.sendEmail(textinput, "test email", "hello", {
name: "Script"
})
}
//-------Sidebar--------
function onOpen() {
FormApp.getUi()
.createMenu('Email Sender')
.addItem('Send message', 'showSidebar')
.addToUi();
}
function showSidebar() {
var html = HtmlService.createHtmlOutputFromFile('index')
.setTitle('Email Sender')
.setWidth(300);
FormApp.getUi()
.showSidebar(html);
}
index.html
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<input type="text" id="textfield" placeholder="Enter email address" value="">
<button onclick="google.script.run.element()">Send</button>
</head>
<body>
</body>
</html>
Solution 1:[1]
It is not defined because your code is only 'aware' of the things inside it, not of the HTML code.
If you want to get the textfield's value, then put that code into the HTML block and pass the value to the function element.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Nick stands with Ukraine |