1

myList contains the following values:

value1
value2
value3

function showArray() {
  var txt = $("#myList").text();
  var textread = txt.split('\n');

  var msg = "";
  for (var i = 0; i < textread .length; i++) {
    msg += i + ": " + textread [i] + "\n";
  }
  alert(msg);
}

my alert gives me the following:

0:value1
value2
value3

It`s not what I wanted and expecting, I was expecting something like:

0: value1
1: value2
2: value3

How can I get the values as expected?

2
  • looks like textread.length==1; could you ouput that too and tell the value here ? Commented Mar 17, 2010 at 11:01
  • Have I tweaked your question correctly or does the first alert show all values on one line? Commented Mar 17, 2010 at 11:04

2 Answers 2

1

I tried this with a textarea and it pretty much worked.

The only thing I changed was var txt = $("#myList").text(); to var txt = $("#myList").val();.

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

1 Comment

This works because val() preserves the white space in the textarea, while text() doesn't.
0

Looks like you have a problem with the character used as line break...

Comments

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.