I'd like to create a simple javascript concatenator that takes 2 HTML textarea tags as input, and concatenates each line from textarea 1 with each line from textarea 2.
So I think I'm basically looking for a javascript function that will generate an array from the textarea, where each new line delimits a new element in the array, like so:
Textbox:
item 1\n
item 2\n
item 3
becomes...
["item 1","item 2","item 3"]
How do I do that? I could do this pretty easily in php, but js mystifies me. If anyone can share the code that would do this, I'd really appreciate it!
Thanks,
Chris
var ary = (
document.getElementById('yourTextArea1').value +
document.getElementById('yourTextArea2').value
).split('\n');
throw an extra + '\n' between those if desired
This should work:
var str = "1\n2\n3 4";
var strArray = str.split("\n");
myArray =(document.getElementById('a').value+document.getElementById('b').value).split('\n');
You can use what #cwolves provided, but it might not work on PCs that use a carriage return instead of a line feed. Here's a script that fixes that and also can join all textareas:
var ta=document.getElementsByTagName("textarea");
var vals=[];
for(var i=0;i<ta.length;i++){
vals.push(ta.value.replace(/\r\n|\n\r/gim,"\n").replace(/\r/gim,"\n");
}
var val=vals.join("\n")
var ary=val.split("\n");
So here the textareas on the page are put into the "ta" variable, then we replace carriage returns with line feeds. The result of it being split into an array is in the "ary" variable.
Ad#m
Related
I'm prefacing this with the fact that coding is most definitely not my strong suit.
I'm currently trying to join two pieces of text to form a link sitting behind an image. The first piece of text is defined (https://www.example.mystore.com/customers/) with the second part being from a datatable (<span id="customcontent752">[Link ID]</span>). Question is - how do I get both of these pieces of information to join to form
https://www.example.mystore.com/customers/[Link ID]? I figure it's something simple I can drop into the source code, but can't for the life of me work it out.
Cheers in advance!
var firstPiece = "https://www.example.mystore.com/customers/";
var secondPiece = $("#customcontent752").text().trim();
var result = firstPiece + secondPiece; //"https://www.example.mystore.com/customers/[Link ID]"
Simply use the + operator if both are strings.
i.e
"https://www.example.mystore.com/customers/" + link_id
should get you what you want.
you can use concat :
var x = "https://www.example.mystore.com/customers/"
var y ="[link ID]"
var result = x.concat(y)
I have strings in the following format in Pentaho Spoon:
"0.31;0.45"
Now I want these to split in Javascript
var str = "0.31;0.45"
var res1 = str.split(";");
However, this returns an array where only the first element is filled as
res1[0] = "0.310.45"
While I'm expecting this:
res1[0] = "0.31"
res1[1] = "0.45"
What am I doing wrong? I've been looking for quite some time now but didn't discover any solution.
The string has been sanitized before, because it was imported from an excel sheet where enters were added between the different values:
str = strold.replace(new RegExp("\n", "gi"), ";");
Snippet of the excel sheet data can be found here (can't disclose complete records due to the sensitive nature of the data):
Exceldata
Any pointers on the right track will be greatly appreciated :)
Thanks!
Rob
If you want to do this in a Modified JavaScript step, this should word fine:
After Excel-Input of the field test1 with value "0.31;0.45" (as string) your JS-Code should look like this:
var str = test1;
var split = str.split(";");
var res1 = split[0];
var res2 = split[1];
The new fields res1 and res2 now show the splitted values. I think your problem was a missing second variable here like split in my code above, maybe a kettle-specific thing...
As a second possible way to solve this without a JavaScript-Step use the Split-Fields-step:
After Excel input set the step Split-Fields. Here set the field to split (in my testscenario test1), set the delimiter (;) and define the two new fields.
Hope it works!
I need to get a id from a html element and replace a part of the word. For example:
HTML
<input type="checkbox" id="facebookCheckbox"></div>
JavaScript
var x = document.getElementById("facebookCheckbox");
var name = x.id;
name.replace("Checkbox","");
This obviously does not work because the replacing word has to be standalone for it to be replaced. Is there a different way of doing this?
I'm looking for purely javascript no jQuery
Thank you!
name.replace("Checkbox","");
This obviously does not work because the replacing word has to be standalone for it to be replaced.
No, it does work and there's no need to be "standalone" - any part of the string can be matched. Only you did nothing with the result of the operation:
console.log(name.replace("Checkbox",""));
// or
name = name.replace("Checkbox","");
// or assign back to x.id maybe?
You are creating a copy of string when replacing, so you must assign the result of .replace() back to x.id.
var x = document.getElementById("facebookCheckbox");
x.id = x.id.replace("Checkbox","");
this is not going to work in this way. However you can have a marker kind of character by which you can break the name into array and implement the logic. For example:
var x = document.getElementById("facebook_Checkbox");
//Note I have added underscore in the Id
var name = x.id;
var arr=name.split("_");
//Now you have Checkbox and Facebook as string objects (part of array) and you can use them
name=arr[0]
I hope it will solve the purpose.
I have a list of names:
Berry, Barry
Altimar, Bonnie
Flopsy, Tonny
Marildoo, Barby
Dalibutton, Fling
These have been populated as through option tags. Via ajax, I have users adding a name to the list. I would like to have it sorted...
How the heck do I split the string?
I did it as
var split = myList.split(",");
alert (split);
Which of course left me with
Berry, BarryAltimar, BonnieFlopsy, TonnyMarildoo, BarbyDalibutton, Fling
Boo.
I can't think of how to split it... I'm sure it's easy, but, as a novice, I'm stumped. I tried using "\n" and "\n\r", but of course those don't work, as the user is not pressing enter.
Any help = greatly appreciated.
var split is an array and you need to access it as split[0]....split[n] instead of split.
Try this code
for(i=0;i<split.length;i++)
alert(split[i]);
By the way mylist itself an array i guess. You should be doing mylist[0].split(',')[0] to get the first row first column value
alert() likely calls function .toString() which returns comma separated values when applied to Array.
Hmmm, how about:
var split = myList.replace(/\,/g, "").split(" ");
jsFiddle example.
im working on an mobile web application, which uses data from a .jsp page in the same directory. I made it possible to get formdata and put it in a variable using jQuery's .val().
now i have created an array of the input, and want to loop it using the $.each.
Only problem is when i try to create the loop, it separates every letter, instead of words..
This is the code i wrote:
var entrylist= $('#entrylistForm').val ();
/* gettin the formvalue. This looks like this:
[{s.ENTRYID=1480565, s.SHEETID=131444777, s.ENTRYDATE=2012-14-04}]
*/
$.each (entrylist, function (key, value) {
alert( key + "=>" + value+"");
// ...
}
I'm tying to get the array like this:
[0]ENTRYID=1480565,
[1]SHEETID=131444777, etc...
help anyone, i cant figure out what i'm doing wrong..
thnx in advance!
Why don't you use the native .split function, and split by whitespace? Assuming the value is a string.
entrylist = entrylist.split(', ');
You should be able to use the split function.
var entrylist = $('#entrylistForm').val();
var list = str.split(', ');
Which would split it after ", " and put it into an array which you can access.
See: http://www.w3schools.com/jsref/jsref_split.asp