0

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!

4
  • Is the intent to not how milk or apple? Or to show them? Commented Dec 2, 2014 at 20:39
  • There's a lot of array functions in JS. You can join your array for example. Take a look here: developer.mozilla.org/en/docs/Web/JavaScript/Reference/… Commented Dec 2, 2014 at 20:39
  • Do you want each array element on a new line? Commented Dec 2, 2014 at 20:40
  • Irfornal --> I want to show them all in the same line (textarea in this case) Commented Dec 2, 2014 at 20:43

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

try ... .join(" ") ... assuming ingArrayTest is the array.
The previous will make one string with SPACES as the separator.
Still doesn't look good. This is the HTML that is generated: <input type="text" id="ingredients" dir="rtl" size="70" value="rice" "oil"="" "soy="" milk"="">
<input type="text" id="ingredients" dir="rtl" size="70" value="rice" "oil" "soy milk">
With my edit, you should be able to examine the string after generation to see what might be causing the issue. Use console.log ...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.