Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have an code.gs that creates a menu that opens a sidebar with html.
My html is like that:
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons1.css">
</head>
<body>
<button onclick='ativarFeedbacks()'>Feedback</button>
<div id="aviso">aaasaaa</div>
<script>
function callback(resposta) {
document.getElementById("aviso").innerHTML=resposta;
}
function ativarFeedbacks() {
google.script.run.withSuccessHandler(callback).escrever();
}
</script>
</body>
</html>
My code.gs is like this:
function onOpen(e) {
FormApp.getUi()
.createAddonMenu()
.addItem('Configurar feedbacks', 'showSidebar')
.addToUi();
}
function showSidebar() {
var ui = HtmlService.createHtmlOutputFromFile('teste2')
.setTitle('Feedback');
FormApp.getUi().showSidebar(ui);
}
function escrever(){
return "<h1> salve salve </h1>"
}
But, when I run onOpen, I open the sidebar and press the button, nothing happens. My innerHTML on id "aviso" doesn't change like nothing was returned by escrever(). I am not understanding this behavior. What should I do?
The function escrever() doesn't exist. Use this instead:
google.script.run.withSuccessHandler(callback).escreverFeedbacks();
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
first time coding java script, I don't quite understand why this simple code wouldn't work with my HTML page. does this syntax looks wrong?
'changecolor' is a 'span id element' in my HTML - its working when i change it with CSS but not with the js below
var txt = document.getElementById('changecolor');
txt.style.color = "red";
I tried calling it as a function, and still nothing
var txt = document.getElementById('changecolor');
function changeColor(){
txt.style.color = "red";
}
changeColor();
I'm 6 hours in learning javascript... and im already lost
Code is correct. You can see example below.
var txt = document.getElementById('changecolor');
function changeColor(){
txt.style.color = "red";
}
changeColor();
<span id='changecolor'>TEST</span>
The problem is that you probably insert JavaScript code before your HTML tags. You should include Javascript at the end of file or wrap everything with ready function.
DomReady.ready(function() {
var txt = document.getElementById('changecolor');
txt.style.color = "red";
}
have you thought about linking your JS script to HTML, we can solve your problem with the following code :
<!DOCTYPE html>
<html>
<head>
<meta charset="utf 8">
<script type="text/javascript" src="js/script.js"></script> <!-- LINK JS -->
<title>Title</title>
</head>
<body>
<h1 class="title">Title</h1>
<p id="txt" onclick="changeColor()">Click me to change my text color.</p>
</body>
function changeColor(){
document.getElementById("txt").style.color = "blue";
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
My code is below. But when I give the above code the element with id "demo" doesn't gets selected. How can I do that in JS?
document.getElementById("demo").onclick = function() {myFunction1()};
function myFunction1() {
document.getElementById("demo").classList.remove("flame");
}
.flame {
color: red;
}
<div class="candle">
<div id="demo" class="flame">Click me</div>
</div>
If my understanding is right, Try using this one
document.getElementById('demo').onclick = function(event) {
callback(event)
}
function callback (event){
var classList = event.target.classList
if (classList.contains('flame')) {
console.log('contains')
classList.remove('flame')
} else {
console.log('notfound')
classList.add('flame')
}
}
.flame {
color: red;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<div class="candle">
<div id="demo" class="flame">Click me</div>
</div>
</body>
</html>
First Why are your using a method to call another function when you can use onclick directly in the html element also make sure you are including the script in a right way since the function function1 needs to be defined before calling it in the Dom element.
So you can use this in the html element
<div class="candle">
<div id="demo" class="flame" onclick="myFunction1()">Hello</div></div>
And for the Javascript:
function myFunction1() {
document.getElementById("demo").classList.remove("flame");}
You can check the fiddle: https://jsfiddle.net/ytdmk6my/2/
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
This following code do nothing .When i click button it should tell my how many guess computer has taken to find my given input. I am using Adobe Dreamweaver.What might be wrong ?
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<p> How many finger ? </p>
<p><input id="myNum" type="text"</p>
<button id="guess"> Mafia </button>
<script type="text/javascript">
document.getElementById('guess').onclick=function(){
var myNumber=document.getElementById('myNum').value;
var GotIt=False;
var NumOfGuess=0;
while(GotIt==False)
{
var guess=Math.random();
guess=guess*6;
guess=Math.floor(guess);
if(guess==myNumber)
{
GotIt==True;
alert("Got It");
alert("You took"+ NumOfGuess +"guess");
}
else
{
NumOfGuess+=1;
}
}
}
</script>
</body>
</html>
Plea check your browser console, you could see that you've some typo's like False/True they should be in lowercase format false/true, e.g:
var GotIt=False;
while(GotIt==Flase){
}
That should be :
var GotIt=false;
while(GotIt==false){
}
Hope this helps.
There is a typo. Replace Flase by false. You should also use === instead of == in your code, but that's secondary.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I did this in JSfiddle:
http://jsfiddle.net/jocelynwang205/qSjn6/
It is supposed to turn a paragraph black, but it seems that the function cannot be called. It does not work on a real webpage either.
Later I added my JavaScript to the html in JSfiddle, like this:
<script>
function changeA(){
document.getElementById('para1').style.color ='black';
}
</script>
<p id="para1">
This is a paragraph to be changed.
</p>
Turn black.
And it worked: http://jsfiddle.net/jocelynwang205/qSjn6/1/
So I want to know why. Thanks in advance.
the original doesnt work because you have onlick instead of onclick, and do not have the code running in the head but in a onload function (settings are on the left side) causing the function to be invisible to the onclick attr
JSFiddle defaults to the javascript being run in a onLoad function so basically your code was being run like below:
<script>
window.onload = function(){
function changeA(){
document.getElementById('para1').style.color ='black';
}
};
<script>
which makes the function changeA invisible to the html onclick attribute
changing the setting to "no wrap - in head" makes it run like:
<script>
function changeA(){
document.getElementById('para1').style.color ='black';
}
<script>
which now makes it visible to the html
Below is a screenshot of where the setting is:
in the jsfiddle you specified onlick instead of onClick.
Turn black.
change it to
Turn black.
in the link, and set option No wrap in from left hand side of jsfiddle.
<!DOCTYPE html >
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<style type="text/css">
#para1
{
color: red;
font-weight: bold;
}
</style>
<script type="text/javascript">
function changeA()
{
document.getElementById('para1').style.color ='black';
}
</script>
</head>
<body>
<p id="para1">This is a paragraph to be changed.</p>
Turn black.
</body>
</html>
Try this.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I have a html page with a button whos onclick calls a javascript function...but I have been getting a function not defined error for two days! Can someone help?
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="css/jquery.mobile-1.2.1.css" />
<link rel="stylesheet" href="css/jqm-docs.css"/>
<script src="js/jquery-1.7.1.min.js"></script>
<script src="js/jqm-docs.js"></script>
<script src="js/jquery.mobile-1.2.1.js"></script>
<script>
function doSomething(){
alert('this is a test');
}
</script>
</head>
<body>
<div data-role="dialog">
<div data-role="content" id = "test" data-theme="c">
$confirm
Cancel
</div>
</div>
</body>
</html>
This might be handy:
http://jquerymobile.com/demos/1.2.0/docs/pages/page-scripting.html
You need to stick your scripts within the page element:
<div data-role="page">
<script>
function doSomething(){
alert('this is a test');
}
</script>
<!-- the rest of your stuff -->
</div>
In fact i have passed personally with this error even with valide HTML all what you need to do is move all of your functions to the head element.
Here is a fiddle:
http://jsfiddle.net/WekEA/1/
To demonstate what i have said in the framework & change the NoWrapToHead to onload