Pavel Zagalsky
Pavel Zagalsky

Reputation: 1636

Adding array that has text with spaces into TextArea using JQuery and append()

I am trying to append to my page an array inside a TextArea and I break it every time I have a space between characters in array. Let's say I have ingredients in array: "rice", "oil", "soy milk", "apple" I am using the following JQuery syntax:

$("#container").append("<input type = 'text' id = 'ingredients' value = " + ingArrayTest+ ">");

My final result will have only: "rice, oil, soy" because space will break the rest of the array in the display. Is there a way to wrap the array so it doesn't happen?

Thanks in advance!

Upvotes: 0

Views: 614

Answers (1)

rfornal
rfornal

Reputation: 5122

You can use a simple array join, assuming I am reading this correctly. Try

ingArrayTest.join("\n")

... in place of ingArrayTest above. Spaces could be used in place of the '\n' ...

BASED ON DISCUSSION, try:

var ingArrayTest = ["Milk", "Soy Milk", "Apple"];
var ingString = ingArrayTest.join(" ");
$("#container").append("<input type='text' id='ingredients' value='" + ingString  + "'>");

...note the single quotes near value. Watch single quotes versus double; very important.

Upvotes: 1

Related Questions