I have a code like this:
<!DOCTYPE html>
<html>
<head>
<script>
function replace(){
var text = document.getElementById("textarea").value;
var a = text.replace("a", "11");
var b = text.replace("b", "12");
var c = text.replace("c", "13");
document.getElementById("textarea").value = abc;
}
</script>
</head>
<body>
<textarea rows="4" cols="50" id="textarea"></textarea>
<button id="button" onclick="replace();">replace</button>
</body>
</html>
I'd like to replace letters with numbers, but I don't know how can I repair this script - only first "var" works or it doesn't work at all.
I think the problem is here:
document.getElementById("textarea").value = abc;
Thank you.
This is a correct way, you need to
<!DOCTYPE html>
<html>
<head>
<script>
function replace(){
var text = document.getElementById("textarea").value;
text = text.replace("a", "11");
text = text.replace("b", "12");
text = text.replace("c", "13");
document.getElementById("textarea").value = text;
}
</script>
</head>
<body>
<textarea rows="4" cols="50" id="textarea"></textarea>
<button id="button" onclick="replace();">replace</button>
</body>
</html>
replace does not change the text
"concatenation" abc didn't make too much sense, but you rather wanted to apply in a sequence 3 transformations
and here is (for a historical reasons :) ) an external playground for you: https://jsfiddle.net/mPrinC/ds9482zn/3/
Related
I am trying to validate user input against field on spreadsheet. Not sure if I am using withFailureHandler correctly. I have an output field defined in the form and this would show 'duplicate entry' if the entry is already present on the spreadsheet. I tried the following code for just testing to see if the form will display any output and this does not seem to be working.
code.gs
function sendItem() {
var form = HtmlService.createHtmlOutputFromFile('test');
SpreadsheetApp.getUi().showSidebar(form);
}
function getTest(sometext) {
return false;
}
test.html
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<div>
<input type="text" id="amount" name="amount" placeholder="serving in gms"><br>
<button class="myStyle" onclick="sendTest()">🫒</button>
</div>
<div id="output" placeholder=" any errors will appear here"></div>
</body>
</html>
<script>
function sendTest(){
var amount = document.getElementById("amount").value;
google.script.run.getTest(amount)
.withFailureHandler(onFailTest);
document.getElementById("amount").value = "";
}
function onFailTest() {
var output = document.getElementById("output");
text = "duplicate entry retry";
output.innerHTML = text;
}
</script>
I'd do the html like this:
html:
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<div>
<input type="text" id="amount" name="amount" placeholder="serving in gms"><br>
<button class="myStyle" onclick="sendTest()">🫒</button>
</div>
<div id="output"></div>
<script>
function sendTest(){
var amount = document.getElementById("amount").value;
google.script.run
.withFailureHandler(function(){output.innerHTML = "duplicate entry retry";})
.getTest(amount);
document.getElementById("amount").value = "";
}
</script>
</body>
</html>
But I'd use withSuccessHandler to handle the getTest return onFailureHandler will handle errors not false returns, I think.
I want to set the values of 2 variables in a gs code to be the text value in 2 text areas on the HTML file.
sidebar.html:
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<input type="button" value="go go go!" onclick="google.script.run.mainFunction();"/>
<strong>Text Area 1</strong>
<textarea id="textarea1" rows="2" cols="35">Text1</textarea>
<strong>Text Area 2</strong>
<textarea id="textarea2" rows="3" cols="35">Text2</textarea>
</body>
</html>
code.gs:
mainFunction() {
var textArea1Value = ???; // should be "Text1"/user's input
var textArea2Value = ???; // should be "Text2"/user's input
// some code
}
How do I achieve this? (what should I write instead of the "???" in the gs code?)
I've tried searching for a solution, but wasn't sure how to implement what I've found as the answers were too specific
Thanks
You need to pass the values as paramaters from the clientside to serverside
For this, obtain the textareas by id within a Javascript code part and pass their values to google.script.run
Sample modification o your code:
sidebar.html:
<input type="button" value="go go go!" onclick="myJSFunction()"/>
<strong>Text Area 1</strong>
<textarea id="textarea1" rows="2" cols="35">Text1</textarea>
<strong>Text Area 2</strong>
<textarea id="textarea2" rows="3" cols="35">Text2</textarea>
<script>
function myJSFunction(){
var text1 = document.getElementById("textarea1").value;
var text2 = document.getElementById("textarea2").value;
google.script.run.mainFunction(text1, text2);
}
</script>
code.gs:
function mainFunction(text1, text2) {
var textArea1Value = text1;
var textArea2Value = text2;
// some code
}
I made a form which have an input text and an input submit,My code is working fine but I want to know how to type in hindi or non-english language in input box ?
Use of input submit is to see what I have typed in input text.
var field1 = document.getElementById('field1');
var resultField = document.getElementById('resultField');
var form = document.getElementById('hindiFont');
form.addEventListener('submit', function(event){
var x = field1.value;
resultField.innerText = x ;
event.preventDefault();
});
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>checking</title>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
</head>
<body>
<form id="hindiFont">
<input type="text" id="field1">
<input type="submit" value="submit">
</form>
<h3 id="resultField"></h3>
<script type="text/javascript" src="form.js"></script>
</body>
</html>
You will have to change your keyboard settings to type in Hindi.
If just want to test, you can copy paste any Hindi text from internet and paste it in the input box
I am loading in a html document using the jsoup.parse(). I only want to modify the url references within the document and then write this out. Unfortunately, the textarea within the document is getting modified also. How can I write out the modified document with only my changes and no other changes? Currently writing out the doc.html().
<textarea class="code_input" id="textareaCode" wrap="logical" rows="10" cols="50">
<!DOCTYPE html>
<html>
<head>
<script>
</script>
</head>
</textarea>
<textarea class="code_input" id="textareaCode" wrap="logical" rows="10" cols="50"><!DOCTYPE html>
<html>
<head>
<script>
</script>
</head>
</html>
</textarea>
I think I understand what you mean. You want to unescape the html entities that are inside the <textarea> so that they are retained as proper tags.
Look into the Parser.unescapeEntities() function (see Jsoup docs).
Example using your sample html:
Document doc = Jsoup.parse(html);
String s = Parser.unescapeEntities(doc.html(), true);
System.out.println(s);
Will print out:
<textarea class="code_input" id="textareaCode" wrap="logical" rows="10" cols="50">
<!DOCTYPE html>
<html>
<head>
<script>
</script>
</head>
</textarea>
Let me know if this works for you.
I am beginner to html and asp. I should write code of the program in Notepad. I want the program that when I click on the Random Button, generate a random number and when I click on the Check button, the program compare between my guess and random number. I write this code but when run the program, not show random number and not compare. Why?
<html>
<head>
<script>
var numOfGuess=new number(0);
var numRandom;
var num;
function RandomNum(){
numRandom=new number(math.floor(math.random()*100));
response.write(numRandom);
numOfGuess=0;
}
fucntion Guess(){
num=document.getElementById("guess");
var alert="";
if(num.value<numRand){
alert="grater than!";
numOfGuess++;
}
esle if (num.value>numRand){
alert="lower than!";]
numOfGuess++;
}
else{
alert="equal!";
numOfGuess++;
}
document.getElementById("message").innerHtml=alert;
}
</script>
</head>
<body>
<input type="text" id="guess">
<input type="submit" onClick='RandomNum()' value="Random">
<p id="message"></p>
<input type="submit" onClick='Guess()' value="Check">
</body>
</html>
Like AnthonyLeGovic said:
you need to be rigorous when programming
here is what you are looking for:
<html>
<head>
<title>test</title>
<script language="javascript">
var numRand = 0;
var numGuess = 0;
var numTry = 1;
function setRand(){
numRand = Math.floor((Math.random()*100)+1);
numTry = 0;
alert("done");
}
function guess(){
var msg = document.getElementById("message");
numGuess=Number(document.getElementById("guess").value);
if(numGuess>numRand){
msg.innerHTML = "lower than!";
}
else if(numGuess<numRand){
msg.innerHTML = "grater than!";
}
else {
msg.innerHTML = "equal! tried " + numTry +"times";
}
numTry++;
}
</script>
</head>
<body>
<input type="text" id="guess" />
<input type="button" onclick="setRand()" value="Random" />
<p id="message"></p>
<input type="button" onclick="guess()" value="Check" />
</body>
do not forget language="javascript" in your script tag else your script will not work!
You need to be rigorous when programming.
There are some syntax errors in your code like :
fucntion instead of function while declaring Guess function.
you declare numRandom as a global variable but you are using numRand then, unfortunately these two variables are not the same at all. Moreover numRand isn't declare (which is logic because it should be numRandom).
you are using innerHtml instead of innerHTML (case sensitive) in order to help the user to find the right number.
Maybe there are some more mistakes I've forgotten right there.