Running Display in textarea - JavaScript - javascript

I was wondering how to keep outputs saved in the textarea box..
I'm writing a javascript program to translate english words to pig latin, so hello would be ellhay.
Anyways, say I type in "are" and in the textarea it translates it to "reaay" and then I type in "hello" (translation "ellohay") the textarea should output "ellohay" and "reaay" under it
I've tried this (edit now trying with array called outputs):
function printLatinWord() {
<!--
var outputs = new Array();
var input = document.form.english.value;
var lowercase = input.toLowerCase();
var fininput = lowercase.split (" ");
var output = "";
for (i = 0; i < fininput.length; i++) {
var result = fininput[i];
output += result.substring (1, result.length) + result.substring(0,1) + "ay ";
document.form.piglat.value = output + "\n";
var j = 0;
output = outputs[j];
j++;
}
/*
var newtext = "\n";
document.form.piglat.value = newtext + document.form.piglat.value;
//trying to keep running display of conversions
var newtext = ("\n");
output += newtext;*/
}
Basically nothing new happens with
At the end the var newtext is supposed to be where the outputs are stored.. but I'm not sure exactly how to get the values from the textarea and keep them there to be displayed under new outputs, if that makes sense.

document.form.piglat.value += newtext; appends newtext to the existing value. If you want it prepended instead, use
document.form.piglat.value = newtext + document.form.piglat.value;

Related

How to find a particular value in a string in js

I have a data like this,
var str = "#data time #city";
My goal is to make this as
var str = <a src="www.test/data">#data</a> time <a src="www.test/city">city</a>
I mean in my string where ever I found # its next value i.e data should be set as a param to the link www.test/{{param}} and should be surrounded with a link.Can any one suggest help.Thanks.
For this case, the String.replace() function will help you:
var str = "#data time #city"
str = str.replace(/#(\S+)/g, '#$1')
output.textContent = str
clickable.innerHTML = str
#output { font-family: courier }
<div id="output"></div>
<div id="clickable"></div>
The following code converts you data to the expected output.
The document.write(..) are for debug.
var data='#data time #city';
var hashtags = data.match(/#\S+/g);
document.write('before: ' + data + '</br>');
for (j = 0; j < hashtags.length; j++) {
// remove # from hashtag
var tag = hashtags[j].substring(1, hashtags[j].length);
// create the link
var link = '< a src="www.test/' + tag + '>#' + tag + '< / a > ';
// replace hashtag with link
data=data.replace(hashtags[j], link);
}
document.write('after: ' + data);

Javascript Addition operator Confusing & Not Working

ok im trying to learn JS but its small thing like this that is making it hard and making me want to say just forget it
for example w3schools says A+=B is the same as saying A = A + B
but when i change this code around to NOT use the A+=B operand it and code it to work as A = A + B doesnt work the same!
so that means A+=B does NOT mean the same as A = A + B then!
heres w3chools example im learning "while" loops at moment and heres is my problem below and this is THEIR code it writes "The Number is 1-19" on a new line each time as it should
<h1>JavaScript while</h1>
<p id="demo"></p>
<script>
var text = "";
var i = 0;
while (i < 10) {
text += "<br>The number is " + i;
i++;
}
document.getElementById("demo").innerHTML = text;
</script>
</body>
</html>
here is MY Code Below and im basicall rewriting it from "A+=B to A = A + B" and it only writes "The Number is" ONE TIME and the number 1-19 on same line!
<h1>JavaScript while</h1>
<p id="demo"></p>
<script>
var text = "<br>The number is ";
var i = 0;
while (i < 10) {
text = text + i;
i++;
}
document.getElementById("demo").innerHTML = text;
</script>
</body>
</html>
You can concat the string each time in the loop:
var text = "";
var i = 0;
while (i < 10) {
var curtext = document
.getElementById("demo")
.innerHTML;
document
.getElementById("demo")
.innerHTML = curtext + "<br>The number is " + i;
i++;
}
<div id="demo"></div>
The difference between you code and W3S's is that you add the i value to text each time not the desired text so the output would be <br>The number is 123456789 to avoid that you should add <br>The number is every time:
var text = '',
text2 = ''
prefix = '<br>The number is',
i = 0;
console.clear();
while (i < 10) {
var toAppend = prefix + i
text += toAppend;
text2 = text2 + toAppend;
i++;
}
document.write(text);
document.write('<br />*****************************');
document.write(text2);

Output in div, prints objects in array, but first answer is "Undefined". - Javascript

I'm very new to this, and I was hoping to get some clarity in this:
Whenever I run this code (PS: boardGames, is an array in a separate doc) It seems to work, but the first answer is always "undefined". Why is that? and how can I fix it?
Thanks!
var message;
var games;
var search;
var i;
function print(message) {
var outputDiv = document.getElementById('output');
outputDiv.innerHTML = message;
}
function gamestoPlay( games ) {
var topGames = '<h3> Game: ' + boardGames[i].name +'</h3>';
topGames += '<p> Minimum Players: ' + boardGames[i].idealMinPlayers + '</p>';
topGames += '<p> Maximum Players: ' + boardGames[i].idealMaxPlayers + '</p>';
return topGames
}
search = parseInt(prompt('How many people are coming?'));
for (i = 0; i < boardGames.length; i += 1) {
games = i;
if ( search >= boardGames[i].idealMinPlayers && search <= boardGames[i].idealMaxPlayers) {
message += gamestoPlay();
print(message);
}
}
Because you didn't initialize message.
When you do
message += gamesToPlay();
it first has to convert message to a string so it can concatenate to it. Since you didn't initialize message, its value is undefined, and when this is converted to a string it becomes "undefined", and then the result of gamesToPlay() is concatenated to that.
Change the initialization to:
var message = "";

Javascript: Looping a Textarea list into an Array

I am trying to retrieve information typed into a Textarea element, load that into an array so that I can output a string. To be more specific, I have an excel sheet with a number, a name, and another number. i want to paste that list into the Textarea, populate an array and output messages custom to the array information per line. (So a message that would have the number, the name, and then another number, go to the next line and do a whole new message with the next three array indexes.)
I can create an array from the Textfield using a for loop and the split() method. Now how do I get Javascript to go to the next line and treat that as a new message and load the new indexes for my next message for as long as the length of the list?
this is my test script so far.
function resultFunction(){
var x = document.getElementById("statusForm");
var text = " ";
var i;
for (i = 0; i < x.length; i++){
text += x.elements[i].value;
}
var loadArray = text.split(",");
var loadID = loadArray[0];
var carrierID = loadArray[1];
var poID = loadArray[2];
console.log(loadArray);
console.log(loadArray.length);
document.getElementById("resultsArea").innerHTML = "Hello " + carrierID + ", <br />Please send me status on the following loads <br />" + loadID + " <br />Thank you!";
};
I think you have to read line by line from text area and split each line into values as you want.
For e.g
var lines = document.getElementById("statusForm").split('\n');
for (i = 0; i < lines .length; i++){
var text = x.elements[i].value;
var loadArray = text.split(",");
var loadID = loadArray[0];
var carrierID = loadArray[1];
document.getElementById("resultsArea").innerHTML = "Hello " + carrierID + ", <br />Please send me status on the following loads <br />" + loadID + " <br />Thank you!";
}
Similiar to the above it will work

Get specific string using javascript

I will briefly explain my issue:
I have a numbers looks like this: 971505896321;971505848963;971505478231;971509856987;
My client should write the above numbers in a text field and I should take the 971505896321 and 971505848963 and 971505478231 and 971509856987 and add them into a list.
I success now to add the numbers to the list but I have difficulties how to get the numbers without ;.
function AddPhoneNo()
{
var recipientNumber = document.smsmobile.mobile_no;
var opt = "<option value='" + recipientNumber.value + "'>" + recipientNumber.value + "</option>"
if (recipientNumber.value != "")
{
if(verifyPhone(recipientNumber.value))
{
$('#selectedOptions').append(opt);
recipientNumber.value = "";
}
}
}
All the numbers should start with 971 and the length of each number is 12. For example: 971506987456.
Appreciate your help.
Thanks,
var recipientNumber = "971505896321;971505848963;971505478231;971509856987;";
var mobileArr = recipientNumber.split(";");
and then u can run a loop on the above array and add those to your options
You just need to replace ; if I m getting you corretly than code for you is
var recipientNumber = document.smsmobile.mobile_no.replace(';','');
EDIT
if you are entering all number one than you need to split out your string
var recipientNumber = "971505896321;971505848963;971505478231;971509856987;";
var arrayofNumbers= recipientNumber.split(";");
var i; for (i = 0; i < arrayofNumbers.length; ++i)
{
var opt = "<option value='" + arrayofNumbers[i]+ "'>" + arrayofNumbers[i]+ "</option>"
if (arrayofNumbers[i] != "")
{
if(verifyPhone(arrayofNumbers[i]))
{
$('#selectedOptions').append(opt);
}
}
}

Categories

Resources