How to get input from textarea to javascript function?
I'm trying to create a pig latin translator that takes input from a textarea and sends it to my javascript function for translation. I hit the submit button and nothing happens when I try it out.
Here is my javascript code:
function translate(String sentence) {
String sentence = document.getElementById("sentence").value;
int wordStart = -1;
int wordEnd = -1;
String letter;
String fullSentence;
for (int i = 0; i < sentence.length(); i++) {
String trans;
char c = Character.toLowerCase(sentence.charAt(i))
if (wordStart === -1 && (c !== ' ' || c !== '.' || c !== ',' || c !== '!' || c !== '?')) {
wordStart = i;
letter = c;
}
if (wordEnd === -1 && (c === ' ' || c === '.' || c === ',' || c === '!' || c === '?' || i === sentence.length() - 1)) {
wordEnd = i;
}
if (wordStart !== -1 && wordEnd !== -1 && (letter !== 'a' || letter !== 'e' || letter !== 'i' || letter !== 'o' || letter !== 'u')) {
trans = sentence.subString(wordStart + 1, wordEnd) + letter + "ay";
wordStart = -1;
wordEnd = -1;
}
if (wordStart !== -1 && wordEnd !== -1 && (letter === 'a' || letter === 'e' || letter === 'i' || letter === 'o' || letter === 'u')) {
trans = sentence.subString(wordStart, wordEnd) + "way";
wordStart = -1;
wordEnd = -1;
}
fullSentence = fullSentence + trans + " ";
}
document.getElementById("output").innerHTML = fullSentence;
}<div style="font-size: 25pt; text-align: center">Pig Latin Translator</div><br /><br />
<div style="font-size: 15pt; text-align: center">
Enter text to be translated:
<form id="piglatin" action="">
<textarea id="sentence" rows="5" cols="30"></textarea><br />
<input type="button" name="submit" value="Submit" onclick="translate('sentence')" />
</form>
</div>
<div id="output"></div>Basically I'm not sure if the problem lies where I'm trying to send the info to my JS function, or if the problem is IN the JS function.
javascript html function textarea
add a comment |
I'm trying to create a pig latin translator that takes input from a textarea and sends it to my javascript function for translation. I hit the submit button and nothing happens when I try it out.
Here is my javascript code:
function translate(String sentence) {
String sentence = document.getElementById("sentence").value;
int wordStart = -1;
int wordEnd = -1;
String letter;
String fullSentence;
for (int i = 0; i < sentence.length(); i++) {
String trans;
char c = Character.toLowerCase(sentence.charAt(i))
if (wordStart === -1 && (c !== ' ' || c !== '.' || c !== ',' || c !== '!' || c !== '?')) {
wordStart = i;
letter = c;
}
if (wordEnd === -1 && (c === ' ' || c === '.' || c === ',' || c === '!' || c === '?' || i === sentence.length() - 1)) {
wordEnd = i;
}
if (wordStart !== -1 && wordEnd !== -1 && (letter !== 'a' || letter !== 'e' || letter !== 'i' || letter !== 'o' || letter !== 'u')) {
trans = sentence.subString(wordStart + 1, wordEnd) + letter + "ay";
wordStart = -1;
wordEnd = -1;
}
if (wordStart !== -1 && wordEnd !== -1 && (letter === 'a' || letter === 'e' || letter === 'i' || letter === 'o' || letter === 'u')) {
trans = sentence.subString(wordStart, wordEnd) + "way";
wordStart = -1;
wordEnd = -1;
}
fullSentence = fullSentence + trans + " ";
}
document.getElementById("output").innerHTML = fullSentence;
}<div style="font-size: 25pt; text-align: center">Pig Latin Translator</div><br /><br />
<div style="font-size: 15pt; text-align: center">
Enter text to be translated:
<form id="piglatin" action="">
<textarea id="sentence" rows="5" cols="30"></textarea><br />
<input type="button" name="submit" value="Submit" onclick="translate('sentence')" />
</form>
</div>
<div id="output"></div>Basically I'm not sure if the problem lies where I'm trying to send the info to my JS function, or if the problem is IN the JS function.
javascript html function textarea
Since you are doing this all client side, why are you wrapping your textarea in a form? Just remove the form, or why are you passing the string 'sentence' to the function when you overwrite it with the value of your textarea? Make the function parameterless. Also as pointed out, you are using the wrong programming syntax.
– Ryan Wilson
Nov 21 '18 at 21:00
You are using mixture of Java. It is not a proper syntax.
– Ziyo
Nov 21 '18 at 21:02
add a comment |
I'm trying to create a pig latin translator that takes input from a textarea and sends it to my javascript function for translation. I hit the submit button and nothing happens when I try it out.
Here is my javascript code:
function translate(String sentence) {
String sentence = document.getElementById("sentence").value;
int wordStart = -1;
int wordEnd = -1;
String letter;
String fullSentence;
for (int i = 0; i < sentence.length(); i++) {
String trans;
char c = Character.toLowerCase(sentence.charAt(i))
if (wordStart === -1 && (c !== ' ' || c !== '.' || c !== ',' || c !== '!' || c !== '?')) {
wordStart = i;
letter = c;
}
if (wordEnd === -1 && (c === ' ' || c === '.' || c === ',' || c === '!' || c === '?' || i === sentence.length() - 1)) {
wordEnd = i;
}
if (wordStart !== -1 && wordEnd !== -1 && (letter !== 'a' || letter !== 'e' || letter !== 'i' || letter !== 'o' || letter !== 'u')) {
trans = sentence.subString(wordStart + 1, wordEnd) + letter + "ay";
wordStart = -1;
wordEnd = -1;
}
if (wordStart !== -1 && wordEnd !== -1 && (letter === 'a' || letter === 'e' || letter === 'i' || letter === 'o' || letter === 'u')) {
trans = sentence.subString(wordStart, wordEnd) + "way";
wordStart = -1;
wordEnd = -1;
}
fullSentence = fullSentence + trans + " ";
}
document.getElementById("output").innerHTML = fullSentence;
}<div style="font-size: 25pt; text-align: center">Pig Latin Translator</div><br /><br />
<div style="font-size: 15pt; text-align: center">
Enter text to be translated:
<form id="piglatin" action="">
<textarea id="sentence" rows="5" cols="30"></textarea><br />
<input type="button" name="submit" value="Submit" onclick="translate('sentence')" />
</form>
</div>
<div id="output"></div>Basically I'm not sure if the problem lies where I'm trying to send the info to my JS function, or if the problem is IN the JS function.
javascript html function textarea
I'm trying to create a pig latin translator that takes input from a textarea and sends it to my javascript function for translation. I hit the submit button and nothing happens when I try it out.
Here is my javascript code:
function translate(String sentence) {
String sentence = document.getElementById("sentence").value;
int wordStart = -1;
int wordEnd = -1;
String letter;
String fullSentence;
for (int i = 0; i < sentence.length(); i++) {
String trans;
char c = Character.toLowerCase(sentence.charAt(i))
if (wordStart === -1 && (c !== ' ' || c !== '.' || c !== ',' || c !== '!' || c !== '?')) {
wordStart = i;
letter = c;
}
if (wordEnd === -1 && (c === ' ' || c === '.' || c === ',' || c === '!' || c === '?' || i === sentence.length() - 1)) {
wordEnd = i;
}
if (wordStart !== -1 && wordEnd !== -1 && (letter !== 'a' || letter !== 'e' || letter !== 'i' || letter !== 'o' || letter !== 'u')) {
trans = sentence.subString(wordStart + 1, wordEnd) + letter + "ay";
wordStart = -1;
wordEnd = -1;
}
if (wordStart !== -1 && wordEnd !== -1 && (letter === 'a' || letter === 'e' || letter === 'i' || letter === 'o' || letter === 'u')) {
trans = sentence.subString(wordStart, wordEnd) + "way";
wordStart = -1;
wordEnd = -1;
}
fullSentence = fullSentence + trans + " ";
}
document.getElementById("output").innerHTML = fullSentence;
}<div style="font-size: 25pt; text-align: center">Pig Latin Translator</div><br /><br />
<div style="font-size: 15pt; text-align: center">
Enter text to be translated:
<form id="piglatin" action="">
<textarea id="sentence" rows="5" cols="30"></textarea><br />
<input type="button" name="submit" value="Submit" onclick="translate('sentence')" />
</form>
</div>
<div id="output"></div>Basically I'm not sure if the problem lies where I'm trying to send the info to my JS function, or if the problem is IN the JS function.
function translate(String sentence) {
String sentence = document.getElementById("sentence").value;
int wordStart = -1;
int wordEnd = -1;
String letter;
String fullSentence;
for (int i = 0; i < sentence.length(); i++) {
String trans;
char c = Character.toLowerCase(sentence.charAt(i))
if (wordStart === -1 && (c !== ' ' || c !== '.' || c !== ',' || c !== '!' || c !== '?')) {
wordStart = i;
letter = c;
}
if (wordEnd === -1 && (c === ' ' || c === '.' || c === ',' || c === '!' || c === '?' || i === sentence.length() - 1)) {
wordEnd = i;
}
if (wordStart !== -1 && wordEnd !== -1 && (letter !== 'a' || letter !== 'e' || letter !== 'i' || letter !== 'o' || letter !== 'u')) {
trans = sentence.subString(wordStart + 1, wordEnd) + letter + "ay";
wordStart = -1;
wordEnd = -1;
}
if (wordStart !== -1 && wordEnd !== -1 && (letter === 'a' || letter === 'e' || letter === 'i' || letter === 'o' || letter === 'u')) {
trans = sentence.subString(wordStart, wordEnd) + "way";
wordStart = -1;
wordEnd = -1;
}
fullSentence = fullSentence + trans + " ";
}
document.getElementById("output").innerHTML = fullSentence;
}<div style="font-size: 25pt; text-align: center">Pig Latin Translator</div><br /><br />
<div style="font-size: 15pt; text-align: center">
Enter text to be translated:
<form id="piglatin" action="">
<textarea id="sentence" rows="5" cols="30"></textarea><br />
<input type="button" name="submit" value="Submit" onclick="translate('sentence')" />
</form>
</div>
<div id="output"></div>function translate(String sentence) {
String sentence = document.getElementById("sentence").value;
int wordStart = -1;
int wordEnd = -1;
String letter;
String fullSentence;
for (int i = 0; i < sentence.length(); i++) {
String trans;
char c = Character.toLowerCase(sentence.charAt(i))
if (wordStart === -1 && (c !== ' ' || c !== '.' || c !== ',' || c !== '!' || c !== '?')) {
wordStart = i;
letter = c;
}
if (wordEnd === -1 && (c === ' ' || c === '.' || c === ',' || c === '!' || c === '?' || i === sentence.length() - 1)) {
wordEnd = i;
}
if (wordStart !== -1 && wordEnd !== -1 && (letter !== 'a' || letter !== 'e' || letter !== 'i' || letter !== 'o' || letter !== 'u')) {
trans = sentence.subString(wordStart + 1, wordEnd) + letter + "ay";
wordStart = -1;
wordEnd = -1;
}
if (wordStart !== -1 && wordEnd !== -1 && (letter === 'a' || letter === 'e' || letter === 'i' || letter === 'o' || letter === 'u')) {
trans = sentence.subString(wordStart, wordEnd) + "way";
wordStart = -1;
wordEnd = -1;
}
fullSentence = fullSentence + trans + " ";
}
document.getElementById("output").innerHTML = fullSentence;
}<div style="font-size: 25pt; text-align: center">Pig Latin Translator</div><br /><br />
<div style="font-size: 15pt; text-align: center">
Enter text to be translated:
<form id="piglatin" action="">
<textarea id="sentence" rows="5" cols="30"></textarea><br />
<input type="button" name="submit" value="Submit" onclick="translate('sentence')" />
</form>
</div>
<div id="output"></div>javascript html function textarea
javascript html function textarea
edited Nov 21 '18 at 21:01
lilelithi
asked Nov 21 '18 at 20:57
lilelithililelithi
194
194
Since you are doing this all client side, why are you wrapping your textarea in a form? Just remove the form, or why are you passing the string 'sentence' to the function when you overwrite it with the value of your textarea? Make the function parameterless. Also as pointed out, you are using the wrong programming syntax.
– Ryan Wilson
Nov 21 '18 at 21:00
You are using mixture of Java. It is not a proper syntax.
– Ziyo
Nov 21 '18 at 21:02
add a comment |
Since you are doing this all client side, why are you wrapping your textarea in a form? Just remove the form, or why are you passing the string 'sentence' to the function when you overwrite it with the value of your textarea? Make the function parameterless. Also as pointed out, you are using the wrong programming syntax.
– Ryan Wilson
Nov 21 '18 at 21:00
You are using mixture of Java. It is not a proper syntax.
– Ziyo
Nov 21 '18 at 21:02
Since you are doing this all client side, why are you wrapping your textarea in a form? Just remove the form, or why are you passing the string 'sentence' to the function when you overwrite it with the value of your textarea? Make the function parameterless. Also as pointed out, you are using the wrong programming syntax.
– Ryan Wilson
Nov 21 '18 at 21:00
Since you are doing this all client side, why are you wrapping your textarea in a form? Just remove the form, or why are you passing the string 'sentence' to the function when you overwrite it with the value of your textarea? Make the function parameterless. Also as pointed out, you are using the wrong programming syntax.
– Ryan Wilson
Nov 21 '18 at 21:00
You are using mixture of Java. It is not a proper syntax.
– Ziyo
Nov 21 '18 at 21:02
You are using mixture of Java. It is not a proper syntax.
– Ziyo
Nov 21 '18 at 21:02
add a comment |
3 Answers
3
active
oldest
votes
Open the Developer Tools in your browser. Look at the Console. You will see numerous error messages.
You appear to be writing Java, but JavaScript is a completely different programming language with little more in common with Java than Carpet does with Car.
The question you ask in the title is one of the things you actually got correct: document.getElementById("sentence").value;
MDN has some introductory JavaScript tutorials. You need to learn that language and not just write Java.
add a comment |
Your code is Java, not javascript.
The code can be rewritten for example like this (syntactically speaking). I'm affraid, it does not do what you expect it to do, but it works and the browser understands it.
function translate() {
var sentence = document.getElementById("sentence").value;
var wordStart = -1;
var wordEnd = -1;
var letter;
var fullSentence;
for (var i = 0; i < sentence.length; i++) {
var trans;
var c = sentence.charAt(i).toLowerCase();
if (wordStart === -1 && (c !== ' ' || c !== '.' || c !== ',' || c !== '!' || c !== '?')) {
wordStart = i;
letter = c;
}
if (wordEnd === -1 && (c === ' ' || c === '.' || c === ',' || c === '!' || c === '?' || i === sentence.length - 1)) {
wordEnd = i;
}
if (wordStart !== -1 && wordEnd !== -1 && (letter !== 'a' || letter !== 'e' || letter !== 'i' || letter !== 'o' || letter !== 'u')) {
trans = sentence.substring(wordStart + 1, wordEnd) + letter + "ay";
wordStart = -1;
wordEnd = -1;
}
if (wordStart !== -1 && wordEnd !== -1 && (letter === 'a' || letter === 'e' || letter === 'i' || letter === 'o' || letter === 'u')) {
trans = sentence.substring(wordStart, wordEnd) + "way";
wordStart = -1;
wordEnd = -1;
}
fullSentence = fullSentence + trans + " ";
}
document.getElementById("output").innerHTML = fullSentence;
}
Looking to some decent JS tutorial as mentioned in other answers would help.
Happy coding
You can experiment with the javascript function for your page fragmet here in JSFidddle I created.
add a comment |
Here is somewhat working code. You still need to modify your logic, because your result is not accurate.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<div style="font-size: 25pt; text-align: center">Pig Latin Translator</div>
<br /><br />
<div style="font-size: 15pt; text-align: center">
Enter text to be translated:
</br>
<textarea id="sentence" rows="5" cols="30"></textarea><br />
<button onclick="Translate()">Submit</button>
</div>
<div id="output"></div>
<script type="text/javascript">
function Translate() {
var sentence = document.getElementById("sentence").value;
var wordStart = -1;
var wordEnd = -1;
var letter;
var fullSentence;
for (let i = 0; i < sentence.length; i++) {
var trans;
var c = sentence.charAt(i).toLowerCase();
if (wordStart === -1 && (c !== " " || c !== "." || c !== "," || c !== "!" || c !== "?")) {
wordStart = i;
letter = c;
}
if (
wordEnd === -1 &&
(c === " " || c === "." || c === "," || c === "!" || c === "?" || i === sentence.length - 1)
) {
wordEnd = i;
}
if (
wordStart !== -1 &&
wordEnd !== -1 &&
(letter !== "a" || letter !== "e" || letter !== "i" || letter !== "o" || letter !== "u")
) {
trans = sentence.substring(wordStart + 1, wordEnd) + letter + "ay";
wordStart = -1;
wordEnd = -1;
}
if (
wordStart !== -1 &&
wordEnd !== -1 &&
(letter === "a" || letter === "e" || letter === "i" || letter === "o" || letter === "u")
) {
trans = sentence.substring(wordStart, wordEnd) + "way";
wordStart = -1;
wordEnd = -1;
}
fullSentence = fullSentence + trans + " ";
}
document.getElementById("output").innerHTML = fullSentence;
}
</script>
</body>
</html>add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53420368%2fhow-to-get-input-from-textarea-to-javascript-function%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Open the Developer Tools in your browser. Look at the Console. You will see numerous error messages.
You appear to be writing Java, but JavaScript is a completely different programming language with little more in common with Java than Carpet does with Car.
The question you ask in the title is one of the things you actually got correct: document.getElementById("sentence").value;
MDN has some introductory JavaScript tutorials. You need to learn that language and not just write Java.
add a comment |
Open the Developer Tools in your browser. Look at the Console. You will see numerous error messages.
You appear to be writing Java, but JavaScript is a completely different programming language with little more in common with Java than Carpet does with Car.
The question you ask in the title is one of the things you actually got correct: document.getElementById("sentence").value;
MDN has some introductory JavaScript tutorials. You need to learn that language and not just write Java.
add a comment |
Open the Developer Tools in your browser. Look at the Console. You will see numerous error messages.
You appear to be writing Java, but JavaScript is a completely different programming language with little more in common with Java than Carpet does with Car.
The question you ask in the title is one of the things you actually got correct: document.getElementById("sentence").value;
MDN has some introductory JavaScript tutorials. You need to learn that language and not just write Java.
Open the Developer Tools in your browser. Look at the Console. You will see numerous error messages.
You appear to be writing Java, but JavaScript is a completely different programming language with little more in common with Java than Carpet does with Car.
The question you ask in the title is one of the things you actually got correct: document.getElementById("sentence").value;
MDN has some introductory JavaScript tutorials. You need to learn that language and not just write Java.
answered Nov 21 '18 at 21:02
community wiki
Quentin
add a comment |
add a comment |
Your code is Java, not javascript.
The code can be rewritten for example like this (syntactically speaking). I'm affraid, it does not do what you expect it to do, but it works and the browser understands it.
function translate() {
var sentence = document.getElementById("sentence").value;
var wordStart = -1;
var wordEnd = -1;
var letter;
var fullSentence;
for (var i = 0; i < sentence.length; i++) {
var trans;
var c = sentence.charAt(i).toLowerCase();
if (wordStart === -1 && (c !== ' ' || c !== '.' || c !== ',' || c !== '!' || c !== '?')) {
wordStart = i;
letter = c;
}
if (wordEnd === -1 && (c === ' ' || c === '.' || c === ',' || c === '!' || c === '?' || i === sentence.length - 1)) {
wordEnd = i;
}
if (wordStart !== -1 && wordEnd !== -1 && (letter !== 'a' || letter !== 'e' || letter !== 'i' || letter !== 'o' || letter !== 'u')) {
trans = sentence.substring(wordStart + 1, wordEnd) + letter + "ay";
wordStart = -1;
wordEnd = -1;
}
if (wordStart !== -1 && wordEnd !== -1 && (letter === 'a' || letter === 'e' || letter === 'i' || letter === 'o' || letter === 'u')) {
trans = sentence.substring(wordStart, wordEnd) + "way";
wordStart = -1;
wordEnd = -1;
}
fullSentence = fullSentence + trans + " ";
}
document.getElementById("output").innerHTML = fullSentence;
}
Looking to some decent JS tutorial as mentioned in other answers would help.
Happy coding
You can experiment with the javascript function for your page fragmet here in JSFidddle I created.
add a comment |
Your code is Java, not javascript.
The code can be rewritten for example like this (syntactically speaking). I'm affraid, it does not do what you expect it to do, but it works and the browser understands it.
function translate() {
var sentence = document.getElementById("sentence").value;
var wordStart = -1;
var wordEnd = -1;
var letter;
var fullSentence;
for (var i = 0; i < sentence.length; i++) {
var trans;
var c = sentence.charAt(i).toLowerCase();
if (wordStart === -1 && (c !== ' ' || c !== '.' || c !== ',' || c !== '!' || c !== '?')) {
wordStart = i;
letter = c;
}
if (wordEnd === -1 && (c === ' ' || c === '.' || c === ',' || c === '!' || c === '?' || i === sentence.length - 1)) {
wordEnd = i;
}
if (wordStart !== -1 && wordEnd !== -1 && (letter !== 'a' || letter !== 'e' || letter !== 'i' || letter !== 'o' || letter !== 'u')) {
trans = sentence.substring(wordStart + 1, wordEnd) + letter + "ay";
wordStart = -1;
wordEnd = -1;
}
if (wordStart !== -1 && wordEnd !== -1 && (letter === 'a' || letter === 'e' || letter === 'i' || letter === 'o' || letter === 'u')) {
trans = sentence.substring(wordStart, wordEnd) + "way";
wordStart = -1;
wordEnd = -1;
}
fullSentence = fullSentence + trans + " ";
}
document.getElementById("output").innerHTML = fullSentence;
}
Looking to some decent JS tutorial as mentioned in other answers would help.
Happy coding
You can experiment with the javascript function for your page fragmet here in JSFidddle I created.
add a comment |
Your code is Java, not javascript.
The code can be rewritten for example like this (syntactically speaking). I'm affraid, it does not do what you expect it to do, but it works and the browser understands it.
function translate() {
var sentence = document.getElementById("sentence").value;
var wordStart = -1;
var wordEnd = -1;
var letter;
var fullSentence;
for (var i = 0; i < sentence.length; i++) {
var trans;
var c = sentence.charAt(i).toLowerCase();
if (wordStart === -1 && (c !== ' ' || c !== '.' || c !== ',' || c !== '!' || c !== '?')) {
wordStart = i;
letter = c;
}
if (wordEnd === -1 && (c === ' ' || c === '.' || c === ',' || c === '!' || c === '?' || i === sentence.length - 1)) {
wordEnd = i;
}
if (wordStart !== -1 && wordEnd !== -1 && (letter !== 'a' || letter !== 'e' || letter !== 'i' || letter !== 'o' || letter !== 'u')) {
trans = sentence.substring(wordStart + 1, wordEnd) + letter + "ay";
wordStart = -1;
wordEnd = -1;
}
if (wordStart !== -1 && wordEnd !== -1 && (letter === 'a' || letter === 'e' || letter === 'i' || letter === 'o' || letter === 'u')) {
trans = sentence.substring(wordStart, wordEnd) + "way";
wordStart = -1;
wordEnd = -1;
}
fullSentence = fullSentence + trans + " ";
}
document.getElementById("output").innerHTML = fullSentence;
}
Looking to some decent JS tutorial as mentioned in other answers would help.
Happy coding
You can experiment with the javascript function for your page fragmet here in JSFidddle I created.
Your code is Java, not javascript.
The code can be rewritten for example like this (syntactically speaking). I'm affraid, it does not do what you expect it to do, but it works and the browser understands it.
function translate() {
var sentence = document.getElementById("sentence").value;
var wordStart = -1;
var wordEnd = -1;
var letter;
var fullSentence;
for (var i = 0; i < sentence.length; i++) {
var trans;
var c = sentence.charAt(i).toLowerCase();
if (wordStart === -1 && (c !== ' ' || c !== '.' || c !== ',' || c !== '!' || c !== '?')) {
wordStart = i;
letter = c;
}
if (wordEnd === -1 && (c === ' ' || c === '.' || c === ',' || c === '!' || c === '?' || i === sentence.length - 1)) {
wordEnd = i;
}
if (wordStart !== -1 && wordEnd !== -1 && (letter !== 'a' || letter !== 'e' || letter !== 'i' || letter !== 'o' || letter !== 'u')) {
trans = sentence.substring(wordStart + 1, wordEnd) + letter + "ay";
wordStart = -1;
wordEnd = -1;
}
if (wordStart !== -1 && wordEnd !== -1 && (letter === 'a' || letter === 'e' || letter === 'i' || letter === 'o' || letter === 'u')) {
trans = sentence.substring(wordStart, wordEnd) + "way";
wordStart = -1;
wordEnd = -1;
}
fullSentence = fullSentence + trans + " ";
}
document.getElementById("output").innerHTML = fullSentence;
}
Looking to some decent JS tutorial as mentioned in other answers would help.
Happy coding
You can experiment with the javascript function for your page fragmet here in JSFidddle I created.
answered Nov 21 '18 at 21:29
BennyHilariousBennyHilarious
659
659
add a comment |
add a comment |
Here is somewhat working code. You still need to modify your logic, because your result is not accurate.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<div style="font-size: 25pt; text-align: center">Pig Latin Translator</div>
<br /><br />
<div style="font-size: 15pt; text-align: center">
Enter text to be translated:
</br>
<textarea id="sentence" rows="5" cols="30"></textarea><br />
<button onclick="Translate()">Submit</button>
</div>
<div id="output"></div>
<script type="text/javascript">
function Translate() {
var sentence = document.getElementById("sentence").value;
var wordStart = -1;
var wordEnd = -1;
var letter;
var fullSentence;
for (let i = 0; i < sentence.length; i++) {
var trans;
var c = sentence.charAt(i).toLowerCase();
if (wordStart === -1 && (c !== " " || c !== "." || c !== "," || c !== "!" || c !== "?")) {
wordStart = i;
letter = c;
}
if (
wordEnd === -1 &&
(c === " " || c === "." || c === "," || c === "!" || c === "?" || i === sentence.length - 1)
) {
wordEnd = i;
}
if (
wordStart !== -1 &&
wordEnd !== -1 &&
(letter !== "a" || letter !== "e" || letter !== "i" || letter !== "o" || letter !== "u")
) {
trans = sentence.substring(wordStart + 1, wordEnd) + letter + "ay";
wordStart = -1;
wordEnd = -1;
}
if (
wordStart !== -1 &&
wordEnd !== -1 &&
(letter === "a" || letter === "e" || letter === "i" || letter === "o" || letter === "u")
) {
trans = sentence.substring(wordStart, wordEnd) + "way";
wordStart = -1;
wordEnd = -1;
}
fullSentence = fullSentence + trans + " ";
}
document.getElementById("output").innerHTML = fullSentence;
}
</script>
</body>
</html>add a comment |
Here is somewhat working code. You still need to modify your logic, because your result is not accurate.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<div style="font-size: 25pt; text-align: center">Pig Latin Translator</div>
<br /><br />
<div style="font-size: 15pt; text-align: center">
Enter text to be translated:
</br>
<textarea id="sentence" rows="5" cols="30"></textarea><br />
<button onclick="Translate()">Submit</button>
</div>
<div id="output"></div>
<script type="text/javascript">
function Translate() {
var sentence = document.getElementById("sentence").value;
var wordStart = -1;
var wordEnd = -1;
var letter;
var fullSentence;
for (let i = 0; i < sentence.length; i++) {
var trans;
var c = sentence.charAt(i).toLowerCase();
if (wordStart === -1 && (c !== " " || c !== "." || c !== "," || c !== "!" || c !== "?")) {
wordStart = i;
letter = c;
}
if (
wordEnd === -1 &&
(c === " " || c === "." || c === "," || c === "!" || c === "?" || i === sentence.length - 1)
) {
wordEnd = i;
}
if (
wordStart !== -1 &&
wordEnd !== -1 &&
(letter !== "a" || letter !== "e" || letter !== "i" || letter !== "o" || letter !== "u")
) {
trans = sentence.substring(wordStart + 1, wordEnd) + letter + "ay";
wordStart = -1;
wordEnd = -1;
}
if (
wordStart !== -1 &&
wordEnd !== -1 &&
(letter === "a" || letter === "e" || letter === "i" || letter === "o" || letter === "u")
) {
trans = sentence.substring(wordStart, wordEnd) + "way";
wordStart = -1;
wordEnd = -1;
}
fullSentence = fullSentence + trans + " ";
}
document.getElementById("output").innerHTML = fullSentence;
}
</script>
</body>
</html>add a comment |
Here is somewhat working code. You still need to modify your logic, because your result is not accurate.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<div style="font-size: 25pt; text-align: center">Pig Latin Translator</div>
<br /><br />
<div style="font-size: 15pt; text-align: center">
Enter text to be translated:
</br>
<textarea id="sentence" rows="5" cols="30"></textarea><br />
<button onclick="Translate()">Submit</button>
</div>
<div id="output"></div>
<script type="text/javascript">
function Translate() {
var sentence = document.getElementById("sentence").value;
var wordStart = -1;
var wordEnd = -1;
var letter;
var fullSentence;
for (let i = 0; i < sentence.length; i++) {
var trans;
var c = sentence.charAt(i).toLowerCase();
if (wordStart === -1 && (c !== " " || c !== "." || c !== "," || c !== "!" || c !== "?")) {
wordStart = i;
letter = c;
}
if (
wordEnd === -1 &&
(c === " " || c === "." || c === "," || c === "!" || c === "?" || i === sentence.length - 1)
) {
wordEnd = i;
}
if (
wordStart !== -1 &&
wordEnd !== -1 &&
(letter !== "a" || letter !== "e" || letter !== "i" || letter !== "o" || letter !== "u")
) {
trans = sentence.substring(wordStart + 1, wordEnd) + letter + "ay";
wordStart = -1;
wordEnd = -1;
}
if (
wordStart !== -1 &&
wordEnd !== -1 &&
(letter === "a" || letter === "e" || letter === "i" || letter === "o" || letter === "u")
) {
trans = sentence.substring(wordStart, wordEnd) + "way";
wordStart = -1;
wordEnd = -1;
}
fullSentence = fullSentence + trans + " ";
}
document.getElementById("output").innerHTML = fullSentence;
}
</script>
</body>
</html>Here is somewhat working code. You still need to modify your logic, because your result is not accurate.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<div style="font-size: 25pt; text-align: center">Pig Latin Translator</div>
<br /><br />
<div style="font-size: 15pt; text-align: center">
Enter text to be translated:
</br>
<textarea id="sentence" rows="5" cols="30"></textarea><br />
<button onclick="Translate()">Submit</button>
</div>
<div id="output"></div>
<script type="text/javascript">
function Translate() {
var sentence = document.getElementById("sentence").value;
var wordStart = -1;
var wordEnd = -1;
var letter;
var fullSentence;
for (let i = 0; i < sentence.length; i++) {
var trans;
var c = sentence.charAt(i).toLowerCase();
if (wordStart === -1 && (c !== " " || c !== "." || c !== "," || c !== "!" || c !== "?")) {
wordStart = i;
letter = c;
}
if (
wordEnd === -1 &&
(c === " " || c === "." || c === "," || c === "!" || c === "?" || i === sentence.length - 1)
) {
wordEnd = i;
}
if (
wordStart !== -1 &&
wordEnd !== -1 &&
(letter !== "a" || letter !== "e" || letter !== "i" || letter !== "o" || letter !== "u")
) {
trans = sentence.substring(wordStart + 1, wordEnd) + letter + "ay";
wordStart = -1;
wordEnd = -1;
}
if (
wordStart !== -1 &&
wordEnd !== -1 &&
(letter === "a" || letter === "e" || letter === "i" || letter === "o" || letter === "u")
) {
trans = sentence.substring(wordStart, wordEnd) + "way";
wordStart = -1;
wordEnd = -1;
}
fullSentence = fullSentence + trans + " ";
}
document.getElementById("output").innerHTML = fullSentence;
}
</script>
</body>
</html><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<div style="font-size: 25pt; text-align: center">Pig Latin Translator</div>
<br /><br />
<div style="font-size: 15pt; text-align: center">
Enter text to be translated:
</br>
<textarea id="sentence" rows="5" cols="30"></textarea><br />
<button onclick="Translate()">Submit</button>
</div>
<div id="output"></div>
<script type="text/javascript">
function Translate() {
var sentence = document.getElementById("sentence").value;
var wordStart = -1;
var wordEnd = -1;
var letter;
var fullSentence;
for (let i = 0; i < sentence.length; i++) {
var trans;
var c = sentence.charAt(i).toLowerCase();
if (wordStart === -1 && (c !== " " || c !== "." || c !== "," || c !== "!" || c !== "?")) {
wordStart = i;
letter = c;
}
if (
wordEnd === -1 &&
(c === " " || c === "." || c === "," || c === "!" || c === "?" || i === sentence.length - 1)
) {
wordEnd = i;
}
if (
wordStart !== -1 &&
wordEnd !== -1 &&
(letter !== "a" || letter !== "e" || letter !== "i" || letter !== "o" || letter !== "u")
) {
trans = sentence.substring(wordStart + 1, wordEnd) + letter + "ay";
wordStart = -1;
wordEnd = -1;
}
if (
wordStart !== -1 &&
wordEnd !== -1 &&
(letter === "a" || letter === "e" || letter === "i" || letter === "o" || letter === "u")
) {
trans = sentence.substring(wordStart, wordEnd) + "way";
wordStart = -1;
wordEnd = -1;
}
fullSentence = fullSentence + trans + " ";
}
document.getElementById("output").innerHTML = fullSentence;
}
</script>
</body>
</html><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<div style="font-size: 25pt; text-align: center">Pig Latin Translator</div>
<br /><br />
<div style="font-size: 15pt; text-align: center">
Enter text to be translated:
</br>
<textarea id="sentence" rows="5" cols="30"></textarea><br />
<button onclick="Translate()">Submit</button>
</div>
<div id="output"></div>
<script type="text/javascript">
function Translate() {
var sentence = document.getElementById("sentence").value;
var wordStart = -1;
var wordEnd = -1;
var letter;
var fullSentence;
for (let i = 0; i < sentence.length; i++) {
var trans;
var c = sentence.charAt(i).toLowerCase();
if (wordStart === -1 && (c !== " " || c !== "." || c !== "," || c !== "!" || c !== "?")) {
wordStart = i;
letter = c;
}
if (
wordEnd === -1 &&
(c === " " || c === "." || c === "," || c === "!" || c === "?" || i === sentence.length - 1)
) {
wordEnd = i;
}
if (
wordStart !== -1 &&
wordEnd !== -1 &&
(letter !== "a" || letter !== "e" || letter !== "i" || letter !== "o" || letter !== "u")
) {
trans = sentence.substring(wordStart + 1, wordEnd) + letter + "ay";
wordStart = -1;
wordEnd = -1;
}
if (
wordStart !== -1 &&
wordEnd !== -1 &&
(letter === "a" || letter === "e" || letter === "i" || letter === "o" || letter === "u")
) {
trans = sentence.substring(wordStart, wordEnd) + "way";
wordStart = -1;
wordEnd = -1;
}
fullSentence = fullSentence + trans + " ";
}
document.getElementById("output").innerHTML = fullSentence;
}
</script>
</body>
</html>answered Nov 21 '18 at 21:30
ZiyoZiyo
21229
21229
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53420368%2fhow-to-get-input-from-textarea-to-javascript-function%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Since you are doing this all client side, why are you wrapping your textarea in a form? Just remove the form, or why are you passing the string 'sentence' to the function when you overwrite it with the value of your textarea? Make the function parameterless. Also as pointed out, you are using the wrong programming syntax.
– Ryan Wilson
Nov 21 '18 at 21:00
You are using mixture of Java. It is not a proper syntax.
– Ziyo
Nov 21 '18 at 21:02